Problem with asp.net mvc modelBinding behavior

被刻印的时光 ゝ 提交于 2020-01-25 18:50:03

问题


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/


回答1:


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.




回答2:


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.




回答3:


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
+ ']'));
        }
    }


来源:https://stackoverflow.com/questions/5201845/problem-with-asp-net-mvc-modelbinding-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!