Model Binders in More Detail
Learn about ASP.NET Core model binders, showing how they assign values to action method parameters from different sources.
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:
<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
, andbool
.Complex types are defined by
class
,record
, orstruct
.Collection types, like
arrays
...