无废话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.代码下载

[点击下载] 

版权:http://www.cnblogs.com/iamlilinfeng

关于App_Code无法编译的问题,请在App_Code文件夹下,鼠标右键-属性-复制->编译

posted @ 2013-03-30 00:03  李林峰的园子  阅读(16671)  评论(16编辑  收藏  举报