本文介绍了如何MVC 4目录模型绑定工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想要一组输入的形式绑定到一个列表在MVC 4,我知道,以下命名约定输入 名称属性将工作:

If I want a set of inputs in a form to bind to a List in MVC 4, I know that the following naming convention for input name attributes will work:

<input name="[0].Id" type="text" />
<input name="[1].Id" type="text" />
<input name="[2].Id" type="text" />

不过,我很好奇如何宽恕模型粘合剂。例如,何谈以下几点:

But I am curious about how forgiving the model binder is. For example, what about the following:

<input name="[0].Id" type="text" />
<input name="[3].Id" type="text" />
<input name="[8].Id" type="text" />

如何将模型绑定处理呢?将其绑定到长9空值的列表?或将它仍然绑定到长度为3的列表?还是会窒息完全?

How would the model binder handle this? Would it bind to a List of length 9 with nulls? Or would it still bind to a List of length 3? Or would it choke altogether?

为什么我不在乎

欲实现动态形式,其中用户可以添加行的形式,并且还可以从表格中删除行。所以,如果我用户删除行2出共8行,我想知道如果我需要重新编排所有的后续投入。

I want to implement a dynamic form in which the user may add rows to the form, and also may delete rows from the form. So if I a user deletes row 2 out of 8 total rows, I want to know if I'll need to renumber all of the subsequent inputs.

推荐答案

有与集合使用特定的传输格式。这是斯科特Hanselman的博客在这里讨论:

There is a specific wire format for use with collections. This is discussed on Scott Hanselman's blog here:

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

从菲尔哈克谈论这这里的另一个博客条目:

Another blog entry from Phil Haack talks about this here:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

最后,不正是你想要的这里的博客条目:

Finally, a blog entry that does exactly what you want here:

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

这篇关于如何MVC 4目录模型绑定工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:48