無廢話MVC入門教程十[實戰二:使用者管理]
阿新 • • 發佈:2018-12-27
MVC入門系列教程-視訊版本,已入駐51CTO學院,文字+視訊學效果更好哦。視訊連結地址如下: 點我檢視視訊。另外,針對該系列教程博主提供有償技術支援,群號:226090960,群內會針對該教程的問題進行及時解答,公用性問題統一講解。
學習.NET MVC 只看在《無廢話系列》足夠了,《無廢話系列》簡單、快速、直接。
學習.NET MVC 只看在《無廢話系列》足夠了,《無廢話系列》簡單、快速、直接。
一、本文目標
學會製做MVC的管理相關功能
二、本文目錄
1.查詢
2.修改
3.刪除
4.程式碼下載
1.查詢
1) View程式碼:
1 @model PagedList<MVC3.DemoModel.User> 2 @using Webdiyer.WebControls.Mvc; 3 @using (Html.BeginForm("Main", "Manage", FormMethod.Get))4 { 5 <span>使用者名稱:</span> 6 @Html.TextBox("username", ViewData["username"]) 7 <input type="submit" value="查詢" /> 8 } 9 @foreach (MVC3.DemoModel.User user in Model) 10 { 11 @user.UserID<span>---</span>@user.UserName<span>---</span> 12@Html.ActionLink("修改", "UserEdit", new { id = user.UserID }) <span>---</span> 13 @Html.ActionLink("詳細", "UserDetail", new { id = user.UserID }) <span>---</span> 14 @Html.ActionLink("刪除", "UserRemove", new { id = user.UserID })<span>---</span> 15 16<br /> 17 } 18 <br /> 19 <br /> 20 @Html.Pager(Model, new PagerOptions 21 { 22 PageIndexParameterName = "id", 23 ShowPageIndexBox = true, 24 FirstPageText = "首頁", 25 PrevPageText = "上一頁", 26 NextPageText = "下一頁", 27 LastPageText = "末頁", 28 PageIndexBoxType = PageIndexBoxType.TextBox, 29 PageIndexBoxWrapperFormatString = "請輸入頁數{0}", 30 GoButtonText = "轉到" 31 }) 32 <br /> 33 >>分頁 共有 @Model.TotalItemCount 篇留言 @Model.CurrentPageIndex/@Model.TotalPageCount
2)Control程式碼:
1 public ActionResult Main(int? id = 1) 2 { 3 ViewData["username"] = string.Empty; 4 if (Request.QueryString["username"] != null) 5 { 6 ViewData["username"] = Request.QueryString["username"].ToString(); 7 } 8 List<Model.User> userList = new List<Model.User>(); 9 int totalCount = 0; 10 int pageIndex = id ?? 1; 11 userList = DemoRepository.User.GetList(ViewData["username"].ToString(), 2, (pageIndex - 1) * 2, out totalCount); 12 PagedList<Model.User> mPage = userList.AsQueryable().ToPagedList(0, 2); 13 mPage.TotalItemCount = totalCount; 14 mPage.CurrentPageIndex = (int)(id ?? 1); 15 return View(mPage); 16 }
3)程式碼解釋:由於mvcPager渲染到客戶端後為<a href="Manage/Main/1">下一頁</a>,所以在查詢時只能以get的方式向伺服器提交查詢條件,在View的程式碼中我們要修改Form的提交方式
1 @using (Html.BeginForm("Main", "Manage", FormMethod.Get))
並且接收到引數後還要回顯到文字框中,在Control與View間我們使用ViewData["username"]做資料傳遞。
4)效果如下:
2.修改
1) View程式碼:
1 @model MVC3.DemoModel.User 2 @using (Html.BeginForm()) 3 { 4 @Html.LabelFor(user => user.UserName) 5 <br /> 6 @Html.TextBoxFor(user => user.UserName) 7 <br /> 8 @Html.LabelFor(user => user.Phone) 9 <br /> 10 @Html.TextBoxFor(user => user.Phone) 11 <br /> 12 @Html.LabelFor(user => user.Residential) 13 @Html.DropDownListFor(user => user.Residential, (SelectList)ViewBag.ViewResidential) 14 @Html.LabelFor(user => user.UnitNo) 15 @Html.DropDownListFor(user => user.UnitNo, (SelectList)ViewBag.ViewUnitNo) 16 @Html.LabelFor(user => user.FloorNo) 17 @Html.DropDownListFor(user => user.FloorNo, (SelectList)ViewBag.ViewFloorNo) 18 @Html.LabelFor(user => user.DoorplateNo) 19 @Html.DropDownListFor(user => user.DoorplateNo, (SelectList)ViewBag.ViewDoorplateNo) 20 <br /> 21 @Html.HiddenFor(user => user.UserID) 22 <input type="submit" value="修改" /> 23 }
2)Control程式碼:
1 public ActionResult UserEdit(int id) 2 { 3 //取出使用者資訊 4 if (id != 0) 5 { 6 Model.User user = DemoRepository.User.Get(new Model.User() { UserID = id }); 7 8 //取出資料,並通過Helper把資料分解 9 AddressHelper addressHelper = AddressHelper.GetInstance(); 10 addressHelper.GetResidetialItem(GetList()); 11 //反選並使用ViewBag傳到View 12 ViewBag.ViewResidential = new SelectList(addressHelper.ResidetialItem, "Value", "Text", user.Residential); 13 ViewBag.ViewFloorNo = new SelectList(addressHelper.FloorNoItem, "Value", "Text", user.FloorNo); 14 ViewBag.ViewUnitNo = new SelectList(addressHelper.UnitNoItem, "Value", "Text", user.UnitNo); 15 ViewBag.ViewDoorplateNo = new SelectList(addressHelper.DoorplateNoItem, "Value", "Text", user.DoorplateNo); 16 return View(user); 17 } 18 return View(); 19 }
3) 程式碼解釋:
1 ViewBag.ViewResidential = new SelectList(addressHelper.ResidetialItem, "Value", "Text", user.Residential);
是為了把資料庫中的資料反選到View上
4)執行效果:
3.刪除
1)View程式碼:
1 @Html.ActionLink("刪除", "UserRemove", new { id = user.UserID }, new { @onclick = "return confirm('確定刪除嗎?');" })<span>---</span>
2)Control程式碼
1 //刪除使用者 2 public void UserRemove(int id, FormCollection formCollection) 3 { 4 Model.User user = new Model.User() { UserID = id }; 5 DemoRepository.User.Remove(user); 6 MessageBox.ShowAndRedirect(this, "刪除成功!", "/Manage/Main/"); 7 }
3)程式碼解釋:
View中加了“確認刪除嗎?”對話方塊。
Ciontrol中:int id, FormCollection formCollection:以HttpPost的方式進行刪除。
4)執行效果:
4.程式碼下載
關於App_Code無法編譯的問題,請在App_Code資料夾下,滑鼠右鍵-屬性-複製->編譯