关于EF4.0中多对多关系的添加与修改的解决方案(MVC)

首先在此声明一下,我不是这方面的高手,也是一个初学者,如果文章中出现不对的地方还请诸位多多指正。
进入正题。
首先我们在数据库中建三个表
新闻表:News
分类表:Categories
新闻与分类的关系表:NewsInCategories
关系如下:

先在数据库中预先添加几个分类:
现在开始添加新闻:
以下是页面代码:

@model MvcRelationShipTest.Models.News @{ ViewBag.Title = "Create"; }

Create

@using (Html.BeginForm()) { @Html.ValidationSummary(true)
News
@Html.LabelFor(model => model.Title)
@Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title)
@Html.LabelFor(model => model.Content)
@Html.EditorFor(model => model.Content) @Html.ValidationMessageFor(model => model.Content)
@Html.LabelFor(model => model.AddTime)
@Html.TextBox("AddTime", DateTime.Now) @Html.ValidationMessageFor(model => model.AddTime)
@Html.Label("分类")
@*Html.CheckBoxFor(Model => Model.Categories)*@ @{Html.RenderAction("ChcekList", "Categories");}

}
@Html.ActionLink("Back to List", "Index")

CheckList的View:
@model IEnumerable
    @foreach (var item in Model) {
  • }
添加页面没什么特别的。

News的Control部分:
//GET public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Models.News model) { try { var t = Request["CategoryId"];//获取选中的分类ID格式为("1,2,3”)不包含括号。 using (var db = new dbContent()) { if (t != null) { List idlist = StringToIntList(t);//将传来的分类ID转成List var cate = db.Categories.Where(p => idlist.Contains(p.Id)); foreach (var item in cate) { model.Categories.Add(item);//给新闻添加分类 } } db.News.AddObject(model); db.SaveChanges(); return RedirectToAction("Index"); } } catch (Exception ex) { var y = ex.InnerException; return View(model); } }
以上是添加新闻的页面代码。
现在修改新闻
Edit View的代码:
@model MvcRelationShipTest.Models.News @{ ViewBag.Title = "Edit"; }

Edit

@using (Html.BeginForm()) { @Html.ValidationSummary(true)
News @Html.HiddenFor(model => model.Id)
@Html.LabelFor(model => model.Title)
@Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title)
@Html.LabelFor(model => model.Content)
@Html.EditorFor(model => model.Content) @Html.ValidationMessageFor(model => model.Content)
@Html.LabelFor(model => model.AddTime)
@Html.EditorFor(model => model.AddTime) @Html.ValidationMessageFor(model => model.AddTime)
@Html.Label("分类")
    @*绑定分类,如果是以选择的 则加载时让他的状态为checked*@ @{List list = (ViewBag.Categories as List); foreach (var item in list) {
  • @if (Model.Categories.Contains(item)) { } else { }
  • } }
@*Action("ChcekList", "Categories")*@

}
@Html.ActionLink("Back to List", "Index")

Controller部分的代码:
public ActionResult Edit(int id) { var db = new dbContent(); ViewBag.Categories = db.Categories.ToList(); var model = db.News.Where(n => n.Id == id).FirstOrDefault(); return View(model); } [HttpPost] public ActionResult Edit(int id, Models.News model) { try { model.Id = id; var t = Request["CategoryId"]; using (var db = new dbContent()) { if (t != null) { List idlist = StringToIntList(t); //新的分类 var cate = db.Categories.Where(p => idlist.Contains(p.Id)).ToList(); //原来的分类 var olist = model.Categories.AsQueryable(); //要删除的分类 var dlist = olist.Where(p => !cate.Contains(p)).ToList(); //要添加的分类 var alist = cate.Where(p => !olist.Contains(p)).ToList(); foreach (var item in dlist) { model.Categories.Remove(item); } foreach (var item in alist) { model.Categories.Add(item); } } else { model.Categories.Clear(); } db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified); db.SaveChanges(); } return RedirectToAction("index"); } catch (Exception ex) { var t = ex.InnerException; var db = new dbContent(); ViewBag.Categories = db.Categories.ToList(); return View(model); } }好了,修改也完成了。
现在分析一下,其实最关键的就是你要把你选择的分类ID以("1,2,3,4,5,9")的方式传到后台。
后台在根据这个分类ID获取分类,然后添加到新闻中,跟web的处理有点类似。
看一下最后的成果,嘻嘻!

想要demo的朋友请加:63181865索取。
到此结束。









你可能感兴趣的:(关于EF4.0中多对多关系的添加与修改的解决方案(MVC))