如何绑定具有子视图模型属性的视图模型?
我很想知道处理以下场景的最佳方法:
public class LittleThingViewModel
{
public string Id{ get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
public class BigThingViewModel
{
public LittleThingViewModel little1 {get; set; }
public LittleThingViewModel little2 {get; set; }
public string propA {get; set; }
public string propB {get; set; }
public string propC {get; set; }
}
在控制器的初始 CREATE 操作中,我填充两个 LittleThingViewModel,将它们分配给一个新的 BigThingViewModel 并将其传递给强类型视图。然后,我使用 EditorFor 渲染 BigThing 属性 A、B 和 C 的编辑控件。
自动将 LittleThingViewModels 渲染到表单以便当我回发到控制器时它会自动绑定的最佳技术是什么? (我的目标是仅显示名称并隐藏其他属性)
public ActionResult Create(BigThingViewModel b)
我发现除了两个 LittleThing 子视图模型的每个单独属性之外,我还可以渲染 A、B 和 C 属性,并且它在 POST 期间成功绑定 BigThingViewModel:
Html.DisplayFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Id)
Html.HiddenFor(model=>model.little1.Location)
是否有一种技术可以让我自动渲染标签和隐藏元素,而不必每次都显式地渲染它们?
比如:
Html.RenderFor(model=>model.little1)
谢谢
I am curious to know the best way to approach the following scenario:
public class LittleThingViewModel
{
public string Id{ get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
public class BigThingViewModel
{
public LittleThingViewModel little1 {get; set; }
public LittleThingViewModel little2 {get; set; }
public string propA {get; set; }
public string propB {get; set; }
public string propC {get; set; }
}
In the initial CREATE action of my controller, I populate two LittleThingViewModels, assign them to a new BigThingViewModel and pass it to the strongly-typed view. I then use EditorFor to render edit controls for BigThing properties A, B, and C.
What is the best technique for automatically rendering the LittleThingViewModels to the form such that when I post back to my controller it auto-binds? (My goal is to only display the Name and keep the other properties hidden)
public ActionResult Create(BigThingViewModel b)
I found that I could render A, B, and C properties in addition to each individual property of the two LittleThing sub-viewmodels and it successfully bind BigThingViewModel during POST:
Html.DisplayFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Id)
Html.HiddenFor(model=>model.little1.Location)
Is there a technique that would allow me to render both labels and hidden elements automatically instead of having to do them each explicitly each time?
Something like:
Html.RenderFor(model=>model.little1)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个强类型化为 LittleThingViewModel 的部分视图。
LittleThingView.ascx(部分视图)
在主视图中
为了使其看起来更好,您可以在大 ViewModel 中为这些对象创建一个集合。因此,如果其中之一为空或者您需要多个,则不需要任何更改。
并在视图中:
Create a partial view that is strongly typed to the LittleThingViewModel.
LittleThingView.ascx (partial view)
In main View
To make it look better you can make a collection for these objects in the big ViewModel. So if one of them is null or you need more than one, it wouldn't need any changes.
And in the view: