學(xué)會(huì )制做MVC的管理相關(guān)功能
1.查詢(xún)
2.修改
3.刪除
4.代碼下載
1) View代碼:
1 @model PagedList<MVC3.DemoModel.User> 2 @using Webdiyer.WebControls.Mvc; 3 @using (Html.BeginForm("Main", "Manage", FormMethod.Get)) 4 { 5 <span>用戶(hù)名:</span> 6 @Html.TextBox("username", ViewData["username"]) 7 <input type="submit" value="查詢(xún)" /> 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 PagerOptions21 {22 PageIndexParameterName = "id",23 ShowPageIndexBox = true,24 FirstPageText = "首頁(yè)",25 PrevPageText = "上一頁(yè)",26 NextPageText = "下一頁(yè)",27 LastPageText = "末頁(yè)",28 PageIndexBoxType = PageIndexBoxType.TextBox,29 PageIndexBoxWrapperFormatString = "請輸入頁(yè)數{0}",30 GoButtonText = "轉到"31 })32 <br />33 >>分頁(yè) 共有 @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)代碼解釋?zhuān)河捎趍vcPager渲染到客戶(hù)端后為<a href="Manage/Main/1">下一頁(yè)</a>,所以在查詢(xún)時(shí)只能以get的方式向服務(wù)器提交查詢(xún)條件,在View的代碼中我們要修改Form的提交方式
1 @using (Html.BeginForm("Main", "Manage", FormMethod.Get))
并且接收到參數后還要回顯到文本框中,在Control與View間我們使用ViewData["username"]做數據傳遞。
4)效果如下:

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 //取出用戶(hù)信息 4 if (id != 0) 5 { 6 Model.User user = DemoRepository.User.Get(new Model.User() { UserID = id }); 7 8 //取出數據,并通過(guò)Helper把數據分解 9 AddressHelper addressHelper = AddressHelper.GetInstance();10 addressHelper.GetResidetialItem(GetList());11 //反選并使用ViewBag傳到View12 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) 代碼解釋?zhuān)?/p>
1 ViewBag.ViewResidential = new SelectList(addressHelper.ResidetialItem, "Value", "Text", user.Residential);
是為了把數據庫中的數據反選到View上
4)運行效果:

1)View代碼:
1 @Html.ActionLink("刪除", "UserRemove", new { id = user.UserID }, new { @onclick = "return confirm('確定刪除嗎?');" })<span>---</span>
2)Control代碼
1 //刪除用戶(hù)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)代碼解釋?zhuān)?/p>
View中加了“確認刪除嗎?”對話(huà)框。
Ciontrol中:int id, FormCollection formCollection:以HttpPost的方式進(jìn)行刪除。
4)運行效果:

聯(lián)系客服