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

时间:2012-05-23 13:16:28

标签: c# .net asp.net-mvc asp.net-mvc-3 razor

我是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,如果有更多模型我如何在多个模型和视图上执行编辑操作?

3 个答案:

答案 0 :(得分:21)

您需要将其他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的子模型并使用EditorFor这样的

@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
}

答案 1 :(得分:8)

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

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)

答案 2 :(得分:0)

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