如何绑定具有子视图模型属性的视图模型?

发布于 2024-11-01 16:06:56 字数 1197 浏览 1 评论 0原文

我很想知道处理以下场景的最佳方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-11-08 16:06:56

创建一个强类型化为 LittleThingViewModel 的部分视图

LittleThingView.ascx(部分视图)

Html.DisplayFor(model=>model.Name)
Html.HiddenFor(model=>model.Name)
Html.HiddenFor(model=>model.Id)
Html.HiddenFor(model=>model.Location)

在主视图中

<%
Html.RenderPartial("LittleThingView",model.little1);
Html.RenderPartial("LittleThingView",model.little2);
%>

为了使其看起来更好,您可以在大 ViewModel 中为这些对象创建一个集合。因此,如果其中之一为空或者您需要多个,则不需要任何更改。

public class BigThingViewModel
{
    public List<LittleThingViewModel> littles=new List<LittleThingViewModel>();
    public string propA {get; set; }
    public string propB {get; set; }
    public string propC {get; set; }
}

并在视图中:

<%
foreach(var littleThingViewModel in model.littles)
{
    Html.RenderPartial("LittleThingView",littleThingViewModel);
}
%>

Create a partial view that is strongly typed to the LittleThingViewModel.

LittleThingView.ascx (partial view)

Html.DisplayFor(model=>model.Name)
Html.HiddenFor(model=>model.Name)
Html.HiddenFor(model=>model.Id)
Html.HiddenFor(model=>model.Location)

In main View

<%
Html.RenderPartial("LittleThingView",model.little1);
Html.RenderPartial("LittleThingView",model.little2);
%>

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.

public class BigThingViewModel
{
    public List<LittleThingViewModel> littles=new List<LittleThingViewModel>();
    public string propA {get; set; }
    public string propB {get; set; }
    public string propC {get; set; }
}

And in the view:

<%
foreach(var littleThingViewModel in model.littles)
{
    Html.RenderPartial("LittleThingView",littleThingViewModel);
}
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文