在MVC中,使用EF对数据库中的数据进行修改真是爽到极点了,下面的一篇文章我总结的是:利用一条数据ID属性,更改里面的内容。这项更改操作用到了强类型的下拉列表框,还有MVC视图中Model这个属性等等。
一、将要执行业务的思维逻辑
1、从数据库搜索出数据来,将数据显示在前台View界面;
2、点击修改按钮,将待修改数据的ID传给Controller里相对应的方法;
3、在上一步的Controller里面按照ID搜索数据库中的内容,将搜索出来的内容赋给一个定义好的实体;
4、将第三步的实体(MVC中的视图有一个model属性)作为参数传递给相对应的Modify.cshtml视图;
5、在Modiy.cshtml中,创建一个表单,表单的提交方式是Post,然后利用第四步传递过来的实体将信息显示出来。
6、在浏览器中修改相应的信息,修改完毕后,点击提交按钮,当表单提交后,会根据指定的控制器(Controller),以及控制器中的方法去修改数据库中的数据。
7、根据提前编写好的代码,根据第六步表单的提交的数据,修改相对应数据库中的内容,最后保存数据库。
二、代码展示
1、控制器中的代码
[HttpGet] //点超链接时,是链接到这 public ActionResult Modify(int id) { //根据ID查询数据库,返回的集合中拿到第一个实体对象 BlogArticle art = (from a in db.BlogArticles where a.AId == id select a).FirstOrDefault(); //生成文章分类下拉框,列表集合中拿到第一个实体对象 IEnumerable<SelectListItem> listItem = (from c in db.BlogArticleCates where c.IsDel == false select c).ToList() .Select(c => new SelectListItem{ Value = c.Id.ToString(), Text = c.Name }); //将生成文章分类的下拉框选项集合设置给ViewBag ViewBag.CateList = listItem; //加载视图,使用View的构造函数,将数据传给视图上的 名为Model的属性 return View(art); } [HttpPost] //点击提交表单时,是执行的这 public ActionResult Modify(BlogArticle model) { try { //1、将实体对象加入EF对象容器中,并获取伪包装类对象 DbEntityEntry<BlogArticle> entry = db.Entry<BlogArticle>(model); //2、将包装类对象的状态设置为 unchanged entry.State = System.Data.EntityState.Unchanged; //3、设置被修改的属性 entry.Property(a => a.ATitle).IsModified = true; entry.Property(a => a.AContent).IsModified = true; entry.Property(a => a.ACate).IsModified = true; //4、提交到数据,完成修改 db.SaveChanges(); return RedirectToAction("Index", "Home"); } catch (Exception ex) { return Content("修改失败!!!" + ex.Message); } }
@model MvcApplication1.Models.BlogArticle <!--指定页面Model属性的类型--> @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>修改</title> <style type="text/css"> #tbList { border:1px solid #0094ff; width:400px; margin:10px auto; /*上下10px;左右水平居中*/ border-collapse:collapse;/*设置为收缩边框*/ } #tbList th, td { border:1px solid #0094ff; padding:10px;/*将内容与单元格填充开*/ } </style> </head> <body> @using (Html.BeginForm("Modify","Home",FormMethod.Post)) { <table id="tbList"> <tr> <td colspan="2">修改 @Html.HiddenFor(a=>a.AId)</td> </tr> <tr> <td>标题:</td> <!--使用HTMLHelper的强类型方法,直接从Model中根据ATitle属性生成文本框--> <td>@Html.TextBoxFor(a=>a.ATitle)</td> </tr> <tr> <td>分类:</td> <!--使用强类型方法生成下拉框,并自动根据model属性里的ACate值 设置下拉框的默认选中项--> <td>@Html.DropDownListFor(a=>a.ACate, ViewBag.CateList as IEnumerable<SelectListItem>)</td> </tr> <tr> <td>内容:</td> <!--使用HTMLHelper的强类型方法,直接从Model中根据AContent属性生成文本域--> <td>@Html.TextAreaFor(a=>a.AContent,10,60,null)</td> </tr> <tr> <td colspan="2"><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Home")</td> </tr> </table> } </body> </html>
1、在视图中,利用HtmlHelper方法跳转页面,利用下面这个方法,其中“返回”是链接的名称,Home 和 Index 是指的视图的路径,也就是Home下面的Index视图
@Html.ActionLink("返回","Index","Home")
2、在Controller中跳转视图
return RedirectToAction("Index", "Home");
3、在JS中,调用Controller的方法,并传递参数。Home代表Controller的名称,Modify代表里面的方法名
window.location = "/Home/Modify/" + id;
4、利用表单提交事件,调用Controller的方法。
@using (Html.BeginForm("Modify","Home",FormMethod.Post))
5、在Controller中,将数据从数据库中搜索出来后,利用View的model属性,将数据传给视图,视图再利用这个属性将数据显示来。
BlogArticle art = (from a in db.BlogArticles where a.AId == id select a).FirstOrDefault(); return View(art);显示数据代码
<!--使用HTMLHelper的强类型方法,直接从Model中根据ATitle属性生成文本框--> <td>@Html.TextBoxFor(a=>a.ATitle)</td>
上面是自己总结的一些零碎的知识点,刚刚接触MVC,感觉自己要从点滴抓起,不能眼高手低,不积跬步无以至千里,不积小流难以成江河啊!