asp.net-mvc Binding to objects


Example

Often you'd be working with viewmodel classes in asp.net-mvc and would want to bind to properties on these. This works similar to mapping to individual parameters.

Say you had a simple view model call PostViewModel like this

public class PostViewModel{
  public int Id {get;set;}
  public int SnappyTitle {get;set;}
}

Then you had posted values of Id and SnappyTitle from a form in the http request then they would map right onto that model if the model itself was the action parameter, e.g.

public ActionResult UpdatePost(PostViewModel viewModel){
  //viewModel.Id would have our posted value
}

It's worth noting the binding is case insensitive for the parameter and property names. It will also cast values where possible. I'm leaving more edge cases for specific examples