朋友炒股兩個月賺了10萬,我幫他推廣一下公眾號,把錢用來投資總比放銀行連通貨膨脹都跑不過里強, 硬核離職,在家炒股 ,這是他每天的日志,有些經驗是花錢也買不到的。
一、本文目標
學會制做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.代碼下載
版權:http://www.cnblogs.com/iamlilinfeng
關於App_Code無法編譯的問題,請在App_Code文件夾下,鼠標右鍵-屬性-復制->編譯