如何在一个单一的剃刀视图编辑多个模型 [英] How to edit multiple models in a single Razor View
问题描述
我是新来MVC3,我有多个模型,如<$c$c>BussinessDetails$c$c>,<$c$c>ContactPerson$c$c>,<$c$c>ServiceArea$c$c>,<$c$c>Address$c$c>和许多更多的车型。我有一个单一的视图页面,共享视图页面就像联系人
, BusinessDetails
,地址
, ServiceArea
等。这些都在标签。他们有自己的模型。
I am new to MVC3, I have an multiple models like BussinessDetails
,ContactPerson
,ServiceArea
,Address
and many more models. I have a single view page where shared view pages like Contacts
,BusinessDetails
,Address
,ServiceArea
etc.these are all in tabs. They have there own models.
我的问题是,如何在同一个编辑视图页编辑多个模型。在发送这个职位,我走了MVC3音乐商店的例子的帮助,但只有一种模式集
,他们给编辑操作在一个模型中,如果有一个或多个模型我怎能在同一视图页面编辑。
My problem is that how to edit multiple models in a same edit view page. Before sending this post I take the help of the MVC3 "Music Store" example but there is only one model ALBUM
and they give edit operation for one model if there is one or more model how I shall edit in the same view page.
我已经做了一个父业务规范类。这是MVC音乐商店
I have already made a parent business specification class. This is from MVC "Music Store"
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
只有在模型集
如果有更多的型号我怎么了上执行编辑操作多个模型和视图?
In HTTP POST
there is only on model ALBUM
if there is more models how i am perform edit operation on multiple models and view?
推荐答案
您需要包括其他的ViewModels成主 CompositeModel
像这样
You need to include the other ViewModels into a main CompositeModel
like so
public class CompositeModel {
public Album AlbumModel { get; set; }
public Another AnotherModel { get; set; }
public Other EvenMore { get; set; }
}
发送,为您的看法,像这样
Send that to your view like so
public ActionResult Index() {
var compositeModel = new CompositeModel();
compositeModel.Album = new AlbumModel();
compositeModel.OtherModel = new AnotherModel();
compositeModel.EvenMore = new Other();
return View(compositeModel)
}
修改您的观点采取新的模式类型
Modify your view to take the new model type
@model CompositeModel
要参考子模型,你可以使用这样的语法属性
To refer to properties of the sub-models you can use syntax like this
@Html.TextBoxFor(model => model.Album.ArtistName)
也可以创建在 EditorTemplates
文件夹中,需要一个子模型如 AlbumModel
视图,并通过< $ C C> EditorFor $像这样
or you can create a view in the EditorTemplates
folder that takes a sub-model like AlbumModel
and use EditorFor
like this
@Html.EditorFor(model => model.Album)
该模板将是这个样子
The template would look something like this
@model AlbumModel
@Html.TextBoxFor(model => model.AlbumName)
@Html.TextBoxFor(model => model.YearReleased)
@Html.TextBoxFor(model => model.ArtistName)
现在刚刚发布 CompositeModel
返回到您的控制器,然后保存所有的子车型,现在鲍勃是你的叔叔!
Now just post CompositeModel
back to your controller and then save all the sub-models and now Bob's your uncle!
[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
}
这篇关于如何在一个单一的剃刀视图编辑多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!