5.显示学员列表
效果
数据表
5.1 首先,在文件夹Models新建一个新建项(W),选择ADO.NET 实体数据模型 (SingleTest.edmx)
5.2 建一个控制器,StudentsController.cs
StudentsController.cs
StudentsController.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace P01FirstMVC.Controllers { public class StudentsController : Controller { //创建数据上下文 Models.SingleTestEntities db = new Models.SingleTestEntities(); /// <summary> /// 显示学院列表 /// </summary> /// <returns></returns> public ActionResult Index() { //1.查询学员数据(EF查询到实体其实是 实体代理类对象(包装类对象)) List<Models.Student> list = (from s in db.Students.Include("class") where s.Isdel == 1 select s).ToList(); //2.加载视图,并将数据传给视图 return View(list); } } }
5.3 再生成一个视图
ViewsStudentsIndex.cshtml
ViewsStudentsIndex.cshtml @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style> #tbList{ border:1px solid #0094ff; width:400px; margin:20px auto; border-collapse:collapse; } #tbList th, td { border: 1px solid #0094ff; padding:2px; } </style> </head> <body> <div> <table id="tbList"> <tbody> <tr> <th>ID</th> <th>姓名</th> <th>班级</th> <th>性别</th> </tr> <!--视图包含一个 Model属性 可以通过控制器 Action 方法里的 View(modeldata)赋值--> @foreach (P01FirstMVC.Models.Student stu in Model as List<P01FirstMVC.Models.Student>) { <tr> <th>@stu.StudentID</th> <th>@stu.Name</th> <th>@stu.Class.CName</th> <th>@stu.Gender</th> </tr> } </tbody> </table> </div> </body> </html>
6.删除
6.1.修改上图
@foreach (P01FirstMVC.Models.Student stu in Model as List<P01FirstMVC.Models.Student>) { <tr> <th>@stu.StudentID</th> <th>@stu.Name</th> <th>@stu.Class.CName</th> <th>@stu.Gender</th> <th><a href="/students/del/@stu.StudentID">删</a></th> </tr> }
6.2.然后再在控制器添加删除方法Del()
StudentsController.cs 2.0 删除
StudentsController.cs 2.0 删除 #region 2.0 删除 指定学员数据 /// <summary> /// 删除 指定学员数据 /// </summary> /// <returns></returns> public void Del(int id) { //2.1 检查id是否存在 //2.2 执行删除 Models.Student stu = new Models.Student() { StudentID = id }; /*等价于下面的entry方法 db.Students.Attach(stu); db.Students.Remove(stu); */ DbEntityEntry<Models.Student> entry = db.Entry<Models.Student>(stu); entry.State = EntityState.Deleted; int reslut = db.SaveChanges(); //return JavaScript("alert('删除成功!');window.location='/Students/Index';"); //return Redirect("/Students/Index");//重定向方法,相当于Response.Redirect("/Students/Index"); Response.Write("<script>alert('删除成功!');window.location='/Students/Index';</script>"); } #endregion
7.修改
ViewsStudentsModify.cshtml
ViewsStudentsModify.cshtml @{ Layout = null; } @using P01FirstMVC.Models; @model P01FirstMVC.Models.Student <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Modify</title> </head> <body> <div> <form action="/Students/Modify" method="post"> <input type="hidden" name="StudentID" value="@Model.StudentID" /> <table> <tbody> <tr> <td>姓名:</td> <td><input name="Name" value="@Model.Name" /></td> </tr> <tr> <td>班级:</td> <td> @*方法二*@ @Html.DropDownList("Cid", ViewBag.classes as IEnumerable<SelectListItem>) @*方法一:查询班级下拉列表 <select name="Cid"> @foreach (Class item in @ViewBag.classes as List<Class>) { //表classes的cid与表student表的cid相等 if (item.Cid==Model.Cid) { <option selected value="@item.Cid">@item.CName</option> } else { <option value="@item.Cid">@item.CName</option> } } </select>*@ </td> </tr> </tbody> </table> <input type="submit" value="OK" /> </form> </div> </body> </html>
StudentsController.cs 3.0 修改
StudentsController.cs 3.0 修改 #region 3.0 修改 /// <summary> /// 修改 /// </summary> /// <returns></returns> public ActionResult Modify(int id) { //3.1.检查id是否存在 //3.2.根据id查询数据 Models.Student stu = (from s in db.Students where s.Isdel == 1 && s.StudentID == id select s).FirstOrDefault(); //3.3.方法一:查询班级下拉列表 //List<Models.Class> classes = (from p in db.Classes where p.CIsdel == "0" select p).ToList(); //ViewBag.classes = classes; //3.3.方法二:查询班级数据并转成下拉框选项 集合 //**************linq查询不识别方法“System.String ToString(Int32)”,因此该方法无法转换为存储表达式。 //*List<SelectListItem> selectClasses = (from p in db.Classes //* where p.CIsdel == "0" //* select new SelectListItem() //* { //* Text = p.CName, //* Value = p.Cid.ToString(), //* Selected = (stu.Cid == p.Cid) //* }).ToList(); //****************************************************************************** List<SelectListItem> cals = db.Classes.Where(p => p.CIsdel == "0").ToList() //选查询数据转成 实体List集合 .Select(p => new SelectListItem() { Text = p.CName, Value = p.Cid.ToString(), //先转成List集合,才能使用ToString() Selected = (stu.Cid == p.Cid) }).ToList(); //将实体集合转成SelectListItem集合 ViewBag.classes = cals; //3.4.加载视图 并传递要修改的值 return View(stu); } //[HttpPost] [HttpPost] public ActionResult Modify(Models.Student model) { DbEntityEntry entry = db.Entry<Models.Student>(model); entry.State = EntityState.Unchanged; entry.Property("Name").IsModified = true; entry.Property("Cid").IsModified = true; db.SaveChanges(); return Redirect("/Students/index"); } #endregion