It is my understanding that the default model binder is capable of turning
<input type="text" name="myType.property1" value="John Doe" />
<input type="text" name="myType.property2" value="22" />
into:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SomeAction(MyType myType)
{
// This will be "John Doe"
String someName = myType.property1;
// This will be 22
Int32 someNumber = myType.property2;
//
// irrelevant magic
}
In what scenario would I use the non-default model binder? I can’t think of a reason not to name the HTML input’s any differently than class property names. Please clarify, with an example.
Thank you!
In the scenario that your
MyTypefor instance doesn’t have a default constructor (the default modelbinder needs a default constructor).This can happen if you use the factory method pattern for creating a new object (very simple example just for illustration ;-):
Then you would have to implement a custom modelbinder which calls the factory method
CreateNewMyType()instead of creating a newMyTypethrough reflection:Also if you’re not happy with the current functionality or implementation of the default modelbinder, then you can easily replace it with your own implementation.