Model Binding is the process by which the incoming parameters from
a HTTP request are mapped to the parameters of the corresponding Action
Methods. The parameters can be route parameters or parameters of the Action
methods. Asp.Net core framework
takes care of matching the parameters from the request with the route/action
method parameters and populated the parameters automatically.
The Framework matched the HTTP parameters with the route/action method parameters using the parameter names, when a parameter name matches it populates the property value from the request and proceeds to match the next parameter.
Model binding maps the values from Form post value, Route values and query string parameter values to the action method. In the below request URL there is an id parameter in the request, MVC will try to match this parameter and map it to the Action method.
http://localhost:52797/UserDetails/Edit?id=6
The parameter matches with the parameter in the below Route Mapping, hence Model binding populates the request parameter to the Route parameter.
routes.MapRoute(
In the POST action method we bind the FORM POST values to the Action Method, the following POST method takes the properties from the form POST and maps them to the User object.
[HttpPost]
The Framework matched the HTTP parameters with the route/action method parameters using the parameter names, when a parameter name matches it populates the property value from the request and proceeds to match the next parameter.
Model binding maps the values from Form post value, Route values and query string parameter values to the action method. In the below request URL there is an id parameter in the request, MVC will try to match this parameter and map it to the Action method.
http://localhost:52797/UserDetails/Edit?id=6
The parameter matches with the parameter in the below Route Mapping, hence Model binding populates the request parameter to the Route parameter.
routes.MapRoute(
name: "UserRoute",
template:
"UserDetails/{action}/{id:int?}",
defaults: new { controller =
"Users", action = "Index" }
);
In the POST action method we bind the FORM POST values to the Action Method, the following POST method takes the properties from the form POST and maps them to the User object.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("UserId,FirstName,LastName,Phone,Email,Address,AttachmentPath")] Users users)
Notice that we are using a [Bind] attribute before the User object, this Attribute specifies to map only the mentioned properties to the User object, if this attribute is not mentioned the framework will try to map all the properties from the request to the User objects properties.
Similar to [Bind] there are few other attributes which can be used to alter the behavior of Model Binding in Asp.Net Core MVC. Following are the attributes.
[BindRequired], [BindNever], [FromHeader], [FromQuery], [FromRoute], [FromForm], [FromServices], [FromBody] & [ModelBinder]
Notice that we are using a [Bind] attribute before the User object, this Attribute specifies to map only the mentioned properties to the User object, if this attribute is not mentioned the framework will try to map all the properties from the request to the User objects properties.
Similar to [Bind] there are few other attributes which can be used to alter the behavior of Model Binding in Asp.Net Core MVC. Following are the attributes.
[BindRequired], [BindNever], [FromHeader], [FromQuery], [FromRoute], [FromForm], [FromServices], [FromBody] & [ModelBinder]
1 comment:
Cara Main Poker Online Uang Asli Tanpa Rekening Bank – Saat ini situs poker online dapat dengan mudah diakses di Internet
asikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna
bocoran sgp
data hk
Post a Comment