如何在单个Razor View中编辑多个模型

我是MVC3的新手,我有多个模型,如BussinessDetailsContactPersonServiceAreaAddress等等。 我有一个单独的视图页面,其中共享视图页面,如ContactsBusinessDetailsAddressServiceArea等这些都在选项卡中。 他们有自己的模特。

我的问题是如何在同一编辑视图页面中编辑多个模型。 在发送这篇文章之前,我接受了MVC3“音乐商店”示例的帮助,但是只有一个模型ALBUM ,如果有一个或多个模型我将在同一个视图页面中编辑,它们会为一个模型提供编辑操作。

我已经创建了一个父业务规范类。 这是来自MVC“音乐商店”

 public ActionResult Edit(int id) { Album album = db.Albums.Find(id); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album); } [HttpPost] public ActionResult Edit(Album album) { if (ModelState.IsValid) { db.Entry(album).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album); } 

HTTP POST ,只有模型ALBUM如果有更多的模型我如何在多个模型和视图上执行编辑操作?

您需要将其他ViewModel包含到主CompositeModel就像这样

 public class CompositeModel { public Album AlbumModel { get; set; } public Another AnotherModel { get; set; } public Other EvenMore { get; set; } } 

像这样发送到您的视图

 public ActionResult Index() { var compositeModel = new CompositeModel(); compositeModel.Album = new AlbumModel(); compositeModel.OtherModel = new AnotherModel(); compositeModel.EvenMore = new Other(); return View(compositeModel) } 

修改视图以采用新模型类型

 @model CompositeModel 

要引用子模型的属性,您可以使用这样的语法

 @Html.TextBoxFor(model => model.Album.ArtistName) 

或者您可以在EditorTemplates文件夹中创建一个视图,该文件夹采用像AlbumModel这样的子模型,并使用这样的AlbumModel

 @Html.EditorFor(model => model.Album) 

模板看起来像这样

 @model AlbumModel @Html.TextBoxFor(model => model.AlbumName) @Html.TextBoxFor(model => model.YearReleased) @Html.TextBoxFor(model => model.ArtistName) 

现在只需将CompositeModel发布回您的控制器,然后保存所有子模型,现在Bob就是您的叔叔!

 [HttpPost] public ActionResult Index(CompositModel model) { // save all models // model.Album has all the AlbumModel properties // model.Another has the AnotherModel properties // model.EvenMore has the properties of Other } 

您需要创建一个包含所需类型的视图模型。 这样的事情(假设你正在编辑专辑和艺术家):

 public class MyModel { public Album Album { get; set; } public Artist Artist { get; set; } public SelectList Genres { get; set; } public SelectList Artists{ get; set; } } 

然后更改您的视图以使用新模型,如下所示:

 @model MyModel 

然后将您的Get方法更改为:

 public ActionResult Edit(int id) { var model = new MyModel(); model.Album = db.Albums.Find(id); model.Artist = yourArtist; //whatever you want it to be model.Genres = new SelectList(db.Genres, "GenreId", "Name", model.Album.GenreId); model.Artists = new SelectList(db.Artists, "ArtistId", "Name", model.Album.ArtistId); return View(model); } 

然后更改Post方法以采用MyModel类型:

 [HttpPost] public ActionResult Edit(MyModel model) { if (ModelState.IsValid) { //save your items here db.SaveChanges(); return RedirectToAction("Index"); } model.Genres = new SelectList(db.Genres, "GenreId", "Name", model.Album.GenreId); model.Artists = new SelectList(db.Artists, "ArtistId", "Name", model.Album.ArtistId); return View(album); } 

假设您的视图有类似的东西(包含在带有提交按钮的表单中):

 @Html.EditorFor(m => m.Artist.Name) //do this for all Artist Fields @Html.EditorFor(m =? m.Album.Name) //do this for all Album Fields //the following two show you how to wire up your dropdowns: @Html.DropDownListFor(m => m.Album.ArtistId, Model.Artists) @Html.DropDownListFor(m => m.Album.GenreId, Model.Genres) 

另一种方法是使用C#元组
http://www.dotnetperls.com/tuple
在视图和控制器中,定义一个元组列表(模型)