如何在 MVC3 中将嵌套的 ViewModel 从 View 绑定到 Controller? [英] How can I bind nested ViewModels from View to Controller in MVC3?

查看:23
本文介绍了如何在 MVC3 中将嵌套的 ViewModel 从 View 绑定到 Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发一个 ASP.NET MVC 3 应用程序,我使用 Razor.我现在正在处理一个关于通过控制器通过视图传递/接收到视图的视图模型绑定对象的问题.让我们说清楚.我有以下 ViewModel:

I am developing an ASP.NET MVC 3 application in C# and I use Razor. I am now dealing with a problem concerning the binding of objects through ViewModels passed/received to/from the View by the Controller. Let's make it clear. I have the following ViewModels:

public class ContainerViewModel
{
   public int ContainerId {get; set;}
   public string ContainerName {get; set;}
   public List<ItemPostModel> ItemData {get; set;}
}

public class ItemPostModel
{ 
   public int ItemId {get; set;}
   public string ItemName {get; set;}
   public int ItemValue {get; set;}
}

ContainerViewModel 用于将数据传递给视图.它的属性 ContainerIdContainerName 仅用于显示目的.List 属性必须使用 Form 填充.视图看起来像这样(它是一个简化版本):

The ContainerViewModel is used to pass the data to the View. Its properties ContainerId and ContainerName are used just for display purposes. The List<ItemPostModel> property has to be filled using a Form. The View looks something like this (it is a simplified version):

<strong>@Model.ContainerName</strong>


@using (Html.BeginForm()) 
{
    <fieldset>
    @foreach(var item in Model.ItemData)
    {
       @Html.TextBox(item.ItemId);
       @Html.TextBox(item.ItemName);
       @Html.TextBox(item.ItemValue);

       <p>
           <input type="submit" value="Save" />
       </p>
    }
    </fieldset>
}

Controller对应的动作方法如下:

public ActionResult UpdateItems()
{
   //fill in the ContainerViewModel lcontainer

   return View("UpdateItems", lcontainer);
}

[HttpPost]
public ActionResult UpdateItems(int containerId, ItemPostModel itemData)
{
   //store itemData into repository
}

问题在于,使用此代码,传递给 Post ActionMethod UpdateItemsItemPostModel itemData 始终为空.containerId 已正确传递.如果我在控制器中使用以下代码,结果相同(显然不是 DRY);

The problem is that with this code the ItemPostModel itemData passed to the Post ActionMethod UpdateItems is always empty. The containerId is correctly passed. Same result if I use the following code in the Controller (obviously not DRY);

[HttpPost]
public ActionResult UpdateItems(ContainerViewModel container)
{
   //extract itemData from ContainerViewModel container
   //store itemData into repository
}

我如何教导"应用程序我希望将表单元素存储在List?我应该修改 ModelBinder 还是有更简单的方法来执行此任务?谢谢大家的回答.

How can I "teach" the application that I want the form elements stored in the List<ItemPostModel>? Shall I modify the ModelBinder or there is a simpler way to perform this task? Thanks everybody for your answers.

推荐答案

不要在视图中编写循环.使用编辑器模板:

Don't write loops in a view. Use editor templates:

<strong>@Model.ContainerName</strong>
@using (Html.BeginForm()) 
{
    <fieldset>
        @Html.EditorFor(x => x.ItemData)
        <input type="submit" value="Save" />
    </fieldset>
}

在相应的编辑器模板(~/Views/Shared/EditorTemplates/ItemPostModel.cshtml)中:

and inside the corresponding editor template (~/Views/Shared/EditorTemplates/ItemPostModel.cshtml):

@model ItemPostModel
@Html.TextBox(x => x.ItemId)
@Html.TextBox(x => x.ItemName)
@Html.TextBox(x => x.ItemValue)

在控制器操作中,您可能需要指定前缀:

And in the controller action you might need to specify the prefix:

[HttpPost]
public ActionResult UpdateItems(
    int containerId, 
    [Bind(Prefix = "ItemData")]ItemPostModel itemData
)
{
   //store itemData into repository
}

这应该就是全部了.编辑器模板将负责为绑定生成正确的输入字段名称.

and that should be pretty much all. The editor template will take care of generating the proper input field names for the binding to work.

这篇关于如何在 MVC3 中将嵌套的 ViewModel 从 View 绑定到 Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆