更新时间:2023-09-24 23:21:52
您需要包括其他的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
视图,并通过 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
}