blog tag

MVC Drop Down Control and Model Binding to Lists

Description: An Html Helper to create a Drop Down List in MVC that binds back to the model
Publish Date: 01/30/2013
Keywords: MVC C# Html Helper Razor Html

In MVC there is no viewstate (webforms) that goes back to the server and tells the model what you had in your drop down list. So on a post where the result might be a return trip with errors, your drop down list would be empty because it did not persist through the HttpPost life cycle.

You could either re-populate the drop down list in the controller or, as this example shows, write an html helper that produces html that binds back to the model so you don't have to remember to repopulate data.

This also is an example of binding Lists to a model simply by incrementing the html names.

Here is where I got my info..

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

 

The Controller..

The Html Helper

The View...

Resulting Html...

Comments: 0
© Chadwick 2021