复杂模型绑定到列表 [英] Complex model binding to a list

查看:26
本文介绍了复杂模型绑定到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试来自 MVCContrib 的 NameValueDeserializer,它将一个 IList 作为控制器的参数并将表单及其元素绑定到它,但我只是想知道 MVC Beta 是否有任何方法可以做到这一点?

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

我知道您可以绑定强类型对象,但我想为某些批量编辑情况绑定这些对象的列表.

I know you can bind a strongly typed Object but I want to bind a List of these Objects for some bulk editing situations.

例如

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

这在 MVC Beta 中可行吗??提前致谢.

Is this possible in MVC Beta?? Thanks in Advance.

推荐答案

是的,我写了一个详细的关于它的博客文章.对于简单类型来说,这真的很容易.对于复杂类型,您需要执行以下操作:

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" />

这篇关于复杂模型绑定到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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