Why isnt my EditorTemplate binding a List on [Http

2019-08-26 16:37发布

问题:

I have uploaded my code to pastebin, this is the link:

http://pastebin.com/wBu9PP2x

When i submit a form, the Lists that i use are not bound to my ViewModel.

But when i send the ViewModel to the view, it renders fine using EditorFor. I have read that when using EditorTemplates, it is supposed to name the List appropriately so that they are bound to the ViewModel automatically upon postback.

The HTML output can be seen here:

http://pastebin.com/5KeyNXWC

Notice that the ViewModel derives from ShowQuestionViewModel, which contains some strings. These strings get bound perfectly.

This is the tutorial i have been following:

http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3

In the tutorial, the MVC framework knows how to bind lists inside of a ViewModel.

Here are some debugger outputs:

Controller takes ShowQuestionViewModel as parameter:

http://imageshack.us/photo/my-images/803/debug.jpg

Controller takes FormCollection as parameter:

http://imageshack.us/photo/my-images/542/formcollection.png

Different Controller that takes a List and FormCollection as parameter:

http://imageshack.us/photo/my-images/685/listtest.png

Dont give up on me guys!!

Thanks!

Solution

I have found this solutin myself. I forgot to use Properties for the rows and columns list in the ShowMatrixQuestionViewModel. Also, the ActionController wont bind without TryUpdateModel() so thanks to @Adam Tuliper as well as the rest.

回答1:

Since you mentioned lists are you sure your model Upon postback contains all of the expected items? Also remembe the HTML helpers will use modelstate to bind data from as well if you are showing data after a post and not redirecting.