开发者

Problem with asp.net mvc modelBinding behavior

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 th开发者_如何转开发e 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/


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜