Tuesday, March 31, 2015

ASP.NET MVC List Model Binding

In my previous post I wrote about ASP.NET MVC Basic Model Binding 

In this post I will talk about list model binding.  Binding list of same type of object is required in many business application either to create bulk records or to update bulk records.

MVC list binding works based on input control naming convention. This can easily be achieved using proper indexing and using For  method.

View Model:
Model contains list of Person property which we wish to bind.

We have BulkPersonCreate action which will list display the bulk create UI. Also we have POST method for saving list of Person.

View code:

Auto generated naming by view engine:

If you open the view source of your page using browser you can see the naming convention used by view engine.

Output of the app:

When user press Save:

It will submit to BulkPersonCreate POST action.
Here is the debugging output on POST action where action parameter is BulkPersonCreateViewModel with user input data:

Now we can use the binded view model for different business scenario of the app.

Download Source