Should a model binder populate all of the model?

Posted by Richard on Programmers See other posts from Programmers or by Richard
Published on 2012-04-12T08:37:14Z Indexed on 2012/04/12 11:42 UTC
Read the original article Hit count: 331

Filed under:
|
|

Should a model binder populate all of the model, or only the bits that are being posted?

For example, I am adding a product in my system and on the form i want the user to select which sites the new product will appear on. Therefore, in my model I want to populate a collection called "AllAvailableSites" to render the checkboxes for the user to choose from. I also need to populate the model with any chosen sites on a post in case the form does not validate, and I need to represent the form showing the initial selections. It would seem that I should let the model binder set the chosen sites on the model, and (once in the controller method) I set the "AllAvailableSites" on the model. Does that sound right? It seems more efficient to set everything in the model binder but someone is suggesting it is not quite right.

I am grateful for any advice; I have to say that all the MVC model binding help online seems to cite really simple examples, nothing complicated.

Do I really need a GET and a POST version of a method? Can't they just take the same view model? Then I check in my model binder if it is a GET/POST, and populate all the model accordingly.

© Programmers or respective owner

Related posts about c#

Related posts about .NET