asp.net mvc modelBinding 行为问题

发布于 2024-10-20 04:12:16 字数 2361 浏览 3 评论 0原文

我有一个复杂的表单

,它允许用户创建一个包含许多活动的 IncomeDeclaration。

当活动的标记如下所示时,这工作得很好:

 <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 动态添加更多活动..它正在渲染新创建的表单元素,如下所示

<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], 1...依此类推,然后我(通过ajax)添加更多表单元素,Post Action实际上只将ajax元素绑定到模型...

所以基本上是一个 IncomeDeclaration,其中包含所有 3 个 元素我在这里使用的活动作为示例只会将第三个(带有随机字符)添加到收入声明中......

我知道它有点复杂,但如果有人知道为什么会发生这种行为,我很感激......

顺便说一句,这就是我使用的。 http:// blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

I have a complex form

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>

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>

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

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

btw, heres what Im using.
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痕至 2024-10-27 04:12:17

您可能想看看这篇文章: http: //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx 最后,Haacked 谈到了他制作的一个助手,用于在项目不是序列时绑定项目。

也就是说,当我有一个复杂的列表时,我通常会采取另一条路线。但它仅适用于启用 JavaScript 的情况。提交时,我构建一个表示列表的 json 对象。然后我使用 json2 库对 json 进行字符串化并将其放置在发布的隐藏字段中。在服务器端,可以使用 JavaScriptSerializer 及其 Deserialize 方法轻松构建列表。

不过,这需要更多的工作。我猜它最终会包含大约 8 行 javascript 和 2 行服务器端代码。

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.

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.

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.

空名 2024-10-27 04:12:17

您可以尝试使用本文中描述的字典模型绑定器 - ASP.NET MVC2 和 MVC3 中的字典模型绑定器

您在代码中唯一应该修复的是不同类型的关键元素。所有密钥应具有相同的类型。

请阅读我对有关 DefaultDictionaryBinder 类源中的建议更改的文章的评论。

You could try using dictionary model binder described in this article - Dictionary Model Binder in ASP.NET MVC2 and MVC3

The only thing you should fix in your code is different types of key elements. All keys should have the same type.

And please read my comment to the article concerning proposed changes in DefaultDictionaryBinder class source.

太傻旳人生 2024-10-27 04:12:17

您需要保持顺序正确。添加新活动后,使用 jquery 按正确的顺序重命名文本框。

这是我使用过的代码。

var tbody = $('#tableId');
    var trs = tbody.children();
    var row = 0;
    for (row = 0; row < trs.length; row++) {
        var txts = trs[row].getElementsByTagName('input');
        for (i = 0; i < txts.length; i++) {
            var name = txts[i].getAttribute('name');

            txts[i].setAttribute('name', name.replace(/\[.*\]/, 'People[' + row
+ ']'));
        }
    }

You need to keep the sequence correct. After adding new activities use jquery to rename textboxes in correct order.

this is the code i have used .

var tbody = $('#tableId');
    var trs = tbody.children();
    var row = 0;
    for (row = 0; row < trs.length; row++) {
        var txts = trs[row].getElementsByTagName('input');
        for (i = 0; i < txts.length; i++) {
            var name = txts[i].getAttribute('name');

            txts[i].setAttribute('name', name.replace(/\[.*\]/, 'People[' + row
+ ']'));
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文