我怎样才能绑定嵌套从View的ViewModels到控制器在MVC3? [英] How can I bind nested ViewModels from View to Controller in MVC3?

查看:102
本文介绍了我怎样才能绑定嵌套从View的ViewModels到控制器在MVC3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在C#中的ASP.NET MVC 3应用程序,我用剃刀。我现在处理涉及的对象穿过的ViewModels的绑定问题/收到/从视图的控制器。
让我们说清楚。我有以下的ViewModels:

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 用于将数据传递给视图。其性能 ContainerId 容器名称使用只是为了显示目的。在 列表&LT; ItemPostModel&GT; 酒店有权使用的表格来填补。视图看起来是这样的(这是一个简化版本):

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>
}

控制器相应的操作方法如下:

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

   return View("UpdateItems", lcontainer);
}

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

问题是,这个code在 ItemPostModel的ItemData 传递给发表ActionMethod UpdateItems 总是空空的。在 containerId 正确传递。同样的结果,如果我用下面的code。在控制器(显然不是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
}

我怎么能的,我想存储在 列表&LT表单元素的应用; ItemPostModel&GT; ?我将修改 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>
}

和相应的编辑模板内(〜/查看/共享/ 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)

和控制器中的动作则可能需要指定preFIX:

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
}

这应该是pretty所有得多。编辑模板将产生正确的输入字段名的绑定工作照顾。

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.

这篇关于我怎样才能绑定嵌套从View的ViewModels到控制器在MVC3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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