[英]In MVC architecture, can a controller and model manage more than one View?
[英]MVC Partial view having more than one model
我有一个对象的树结构(如下图所示)。 Descendant1
和Descendant2
类的查看方式与Form
类(根类)相同。 我想为此创建一个局部视图,但是,我需要在视图中有一个模型,因为我正在使用模型中的属性(例如根类中的名称)
这是我要在局部视图中的 FormView 代码:
<div>
@foreach (var subForm in Model.SubForms)
{
Html.RenderPartial(partialView, subForm);
}
<div>@Model.Name</div>
</div>
我想知道是否可以创建一个可以有多个模型的局部视图? 我是否为所有后代类一起创建一个控制器? 否则,您对如何进行有任何想法吗?
我想知道是否可以创建一个可以有多个模型的局部视图?
是的,为这 3 个类创建一个视图模型。 下面的示例名为FormViewModel
。
public class FormViewModel{
public class Descendant1 {get;set;}
public class Descendant2 {get;set;}
public class Descendant3 {get;set;}
}
那么在你看来你可以这样做;
@foreach (var subForm in Model.SubForms)
{
FormViewModel fmv = new FormViewModel();
// access its class properties
fmv.Descendant1.SubForms.Add(new Form(){ // assign properties });
// pass the view model to the partial view
Html.RenderPartial(partialView, fmv);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.