Search⌘ K
AI Features

Model Binders in More Detail

Explore the role of model binders in ASP.NET Core MVC to automatically populate action method parameters from HTTP requests. Understand how data is bound from routes, query strings, and form submissions. Learn to manage action method conflicts by specifying HTTP verbs and practice creating complex model types for binding in web applications.

Model binders are a powerful yet easy way to set parameters of action methods based on values passed in an HTTP request, and the default one does a lot for us. After the default route identifies a controller class to instantiate and an action method to call, if that method has parameters, then those parameters need to have values set.

Model binders do this by looking for parameter values passed in the HTTP request as any of the following types of parameters:

  • Route parameter, like id, as we used in the previous section, as shown in the following URL path: /Home/ProductDetail/2

  • Query string parameter, as shown in the following URL path: /Home/ProductDetail?id=2

  • Form parameter, as shown in the following markup:

HTML
<form action="post" action="/Home/ProductDetail">
<input type="text" name="id" value="2" />
<input type="submit" />
</form>

Model binders can populate almost any type:

  • Simple types, like int, string, DateTime, and bool.

  • Complex types are defined by class ...