A form in my MVC4 application shows order information. The model for the view is OrderViewModel. It has a property Orderlines which is a collection of order lines.
I'm using MvcContrib Grid to show the order lines. When the form is submitted, the following controller method executes:
[HttpPost]
public ActionResult PlaceOrder(OrderViewModel model)
{
...
}
My problem is that the Orderlines property is always null in the incoming model parameter. Other fields like customer name are bound from the view to the view model, but the orderlines collection is not. Is there a way to bind the data from the grid to the view model that is sent back to the controller on postback?
Regards, Nils
You can always create a custom model binder and have your data populated. This is especially useful when working with inner collection properties and complex objects. Implementing this is very simple, once you figure it out. You need to implement two classes
CustomModelBinderAttribute
andIModelBinder
.Your final code will look something like this: