简体   繁体   English

复杂模型绑定到列表

[英]Complex model binding to a list

I have been trying out the NameValueDeserializer from MVCContrib, which will take a IList as a parameter to a controller and bind a form and its elements to it, but I was just wondering if MVC Beta had any way of doing this??我一直在尝试来自 MVCContrib 的 NameValueDeserializer,它将 IList 作为控制器的参数并将表单及其元素绑定到它,但我只是想知道 MVC Beta 是否有任何方法可以做到这一点?

I know you can bind a strongly typed Object but I want to bind a List of these Objects for some bulk editing situations.我知道您可以绑定强类型对象,但我想为一些批量编辑情况绑定这些对象的列表。

eg.例如。

public void Save(IList<Item> items)
{
    foreach (Item i in items)
    {
        //Save item
    }
}

Is this possible in MVC Beta??这在 MVC Beta 中可能吗?

Yes it is, I wrote a detailed blog post about it here .是的,我在这里写了一篇详细的博客文章 It's really easy for simple types.对于简单的类型来说真的很容易。 For complex types, you'd need to do something like:对于复杂类型,您需要执行以下操作:

<input type="hidden" name="products.Index" value="0" />
<input type="text" name="products[0].Name" value="Beer" />
<input type="text" name="products[0].Price" value="7.32" />

<input type="hidden" name="products.Index" value="1" />
<input type="text" name="products[1].Name" value="Chips" />
<input type="text" name="products[1].Price" value="2.23" />

<input type="hidden" name="products.Index" value="2" />
<input type="text" name="products[2].Name" value="Salsa" />
<input type="text" name="products[2].Price" value="1.23" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM