asp.net MVC模型绑定问题 [英] Problem with asp.net mvc modelBinding behavior
问题描述
我的表格很复杂
它允许用户创建一个包含许多活动的收入声明.
It allows the user to create an IncomeDeclaration which has many Activities.
当活动"的标记如下时,此方法可以正常工作:
This works fine when the markup for the Activities is like this:
<tr>
<td>SomeActivity
<input type="hidden" value="13123" name="EconomicActivityIncomeDeclarations[0].EconomicActivityId" id="EconomicActivityIncomeDeclarations_0__EconomicActivityId">
</td>
<td>
<input type="text" name="EconomicActivityIncomeDeclarations[0].GrossIncome" id="EconomicActivityIncomeDeclarations_0__GrossIncome" />
</td>
</tr>
<tr>
<td>SomeActivity
<input type="hidden" value="654654" name="EconomicActivityIncomeDeclarations[1].EconomicActivityId" id="EconomicActivityIncomeDeclarations_1__EconomicActivityId">
</td>
<td>
<input type="text" name="EconomicActivityIncomeDeclarations[1].GrossIncome" id="EconomicActivityIncomeDeclarations_1__GrossIncome" />
</td>
</tr>
问题是我正在通过javascript ..动态添加更多活动,这正在呈现新创建的表单元素,例如
The problem is I am dynamically adding more activities through javascript.. which is rendering the newly created form elements like this
<tr>
<td>SomeActivity
<input type="hidden" value="987987" name="EconomicActivityIncomeDeclarations[1b117bc9-ce4b-46d5-9de0-77ba98b82fd0].EconomicActivityId" id="EconomicActivityIncomeDeclarations_1b117bc9-ce4b-46d5-9de0-77ba98b82fd0__EconomicActivityId">
</td>
<td>
<input type="text" name="EconomicActivityIncomeDeclarations[1b117bc9-ce4b-46d5-9de0-77ba98b82fd0].GrossIncome" id="EconomicActivityIncomeDeclarations_1b117bc9-ce4b-46d5-9de0-77ba98b82fd0__GrossIncome" />
</td>
</tr>
我得到的怪异行为是,当前两个(或多个)具有常规格式[0]时,
The weird behavior I get is that when the first two (or more) have the regular format [0], 1... and so on and then I (through ajax) add more form elements the Post Action actually only binds the ajax elements to the Model...
因此,基本上,在这里以Im为例的具有Im所有3个活动的IncomeDeclaration中,只会将第三个(带有随机字符)添加到IncomeDeclaration中....
So basically an IncomeDeclaration which has all 3 of the activities Im using here as example will only get the THIRD one (with the random characters) added to the IncomeDeclaration....
我知道它有点复杂,但是如果有人知道为什么会发生这种行为,我会很感激...
I know its a little complicated but if anyone knows why this behavior is happening I appreciate it...
顺便说一句,这是我正在使用的东西. http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
btw, heres what Im using. http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
推荐答案
You may want to look at this post: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx in the end Haacked talks about a helper he made to bind items when they aren't a sequence.
这表示当我有一个复杂的清单时,我通常会走另一条路线.不过,它将仅在启用了javascript的情况下工作.在提交时,我构建了一个代表该列表的json对象.然后,我使用json2库对json进行字符串化,并将其放置在发布的隐藏字段中.在服务器端,使用JavaScriptSerializer及其Deserialize方法很容易构建列表.
That said I usually take another route when I have a complex list. It will only work with javascript enabled though. On submit I build a json object representing the list. Then I use the json2 library to stringify the json and place it in a hidden field which get's posted. On the serverside it's easy to build the list by using the JavaScriptSerializer and it's Deserialize method.
这还需要更多工作.我猜想它最终将出现在大约8行的javascript和2行的服务器端代码中.
This is a bit more work though. I would guess it ends up at about 8 rows of javascript and 2 rows of serverside code.
这篇关于asp.net MVC模型绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!