asp.net mvc: Why doesn’t Id get bound?

This is one of those ‘reminder for myself’ type posts. In asp.net mvc a form can be posted (or getted) to a controller action which has a complex type parameter. Asp.net MVC will automatically convert the form values to an instance of the complex type by using the DefaultModelBinder class. Scott Hanselman provides the best documentation of this behaviour.

From time to time I find that I have properties on my action parameter that don’t get bound. Most commonly for me this occurs when the action parameter type as a property called ‘Id’. The reason that ‘Id’ does not get bound is because the MVC template defines a default route with the following patter: {controller}/{action}/{id}. Values that arrive as part of the url get precedence over form values, thus if you have a form value with the same name as a route value the form value will not get bound.


Comments are closed