2012-05-23 54 views
10

我是MVC3的新手,我有多個模型,如BussinessDetailsContactPerson,ServiceArea,Address以及更多模型。我有一個單一的查看頁面,共享查看頁面,如Contacts,BusinessDetails,Address,ServiceArea等。這些都在選項卡中。他們有自己的模型。如何在單個剃刀視圖中編輯多個模型

我的問題是如何在同一個編輯視圖頁面中編輯多個模型。在發送這篇文章之前,我藉助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如果有更多的型號如何我多模型進行編輯操作和查看?

回答

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); 
} 

然後更改發佈你的方法採取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) 
+1

什麼IM寫入後,如果(ModelState中的列表。 IsValid){ 來編輯特定的模型。 – user1196392

+0

@ user1196392你不能在你的數據類上調用類似'Save'的東西嗎?類似於model.Album.Save();'爲模型中的每種類型。 – mattytommo

20

您需要包括其他的ViewModels成主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回到你的控制器,然後保存所有的子模型,現在鮑勃的你的叔叔!

[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 
} 
+0

此解決方案對我無效 – user1196392

+1

這只是僞代碼,不能從字面上理解,您必須弄清楚如何更正它,或發佈您正在嘗試的新代碼,以便我們可以進一步提供幫助。保持反饋循環:-) –