In this particular code:
[HttpPost]
public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
{
return View("Customer", cust);
}
These are the parts:
[HttpPost]
is a method attribute. It modifies the method with additional meta-data or meta-functionality. In this case it restricts HTTP access to that action to only POST
verbs.
[ModelBinder(typeof(CustomerBinder))]
is another attribute, but this modifies the particular argument to the method instead of the method itself. In this case the Customer cust
argument. The ModelBinder
attribute allows you to explicitly specify a model binder to use, so you can provide custom model binders for particular actions.
Essentially, MVC (and WebAPI) examines the incoming form values in the HTTP request and makes its best effort (using default model binders) to apply those values to the method arguments. In the vast majority of cases, this works just fine. Sometimes, however, you may want to implement custom functionality for this. So you can write your own model binders, and there are varying ways to tell the framework to use them. This is one such way, which applies a specific model binder (CustomerBinder
) to a specific argument of only this specific method.