Model Binding in ASP.NET MVC
Ever since I started discovering the MVC world I decided to completely forget Web Forms and actually adopt asp.net MVC as my web programming framework of choice. What’s great about it is that you have complete control over the generated html output and absolute separation of business logic, presentation layer and model. It’s all about convention over configuration! This is one of the reasons why I decided to learn Ruby on Rails some time ago. It is clear that the
ASP.NET MVC .NET Framework 3.5 SP1 release was somewhat influenced by Rails.
One of the great things about ASP.NET MVC is model binding. K. Scott Allen wrote a great blog post with tips on binding. Basically, it collects your request data and fills up your view model with the incoming data.
There is a search pattern that the DefaultModelBinder uses to look for the data in the request. Basically you need to name your form elements with the same name of your model properties. For example:
- The view model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(I’ve reduced the HTML markup snippet for the sake of simplicity).
Please note that the input field name properties match the view model property names. The DefaultModelBinder will look for these names when doing the data binding!
You can even bind complex objects like lists, for example, but, in this case, you’ll have to name your input fields according to the element index on the list! Eg.:
<span class="lnum"> 1: </span><span class="kwrd"><</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">="text"</span> <span class="attr">name</span><span class="kwrd">="<strong>ImageList.FileName</strong>" size="</span><span class="attr">44</span>" <span class="kwrd">/></span>
It is really straightforward once you get used to it. Just always make sure to create a ViewModel class for you, instead of using the data classes directly (eg.: with LINQ to SQL).
If you need to bind uploaded files as well, it gets a bit more complicated. The best solution I’ve found so far was not to bind file input control and later retrieve the uploaded file in the controller action from the Request.Files collection.