Categories
ASP.NET Core

Model Binding in ASP.NET Core

In the ASP.NET core, the razor page uses the HTTP request data. The model binding helps to get the data from various sources. It converts string data to .NET data type and provides it to controller and razor pages. Previously, the developer should extract the contents from the HTTP request and manually assign the value to the properties. But the model binding simplifies the work. It automatically binds the browser data to the controller action parameter.

How Model Binding Works

Consider the following controller action. The action method Contact has two parameters ID and name. The ID parameter is an integer and, the name parameter is a string.

[HttpGet]
public ActionResult Contact(int ID, string name)
{
——
return View();
}

Imagining the HTTP request is ~/Contact?id=5&name=harry. The above Contact action method will bind the value 5 to the ID parameter and, harry to name parameter. First, the model binding will find the first parameter of the contact method, in this scenario, an integer ID. Then it will check the request and find the value for the parameter. The ID parameter in the request is a string.

So the model binding will convert the string ID 5 to an integer. Then it will check for the next parameter name and find the value harry, as it is a string it will bind the value to the parameter. Here the binding is not case sensitive. So the action parameter can be mentioned as ID or id. The model binder will bind the null value to the parameter if there is no value for the parameter. For example, if the HTTP request is ~/Contact?id=5 then it will pass the null value to the name parameter. But imagine, the request is ~/Contact?name=harry then it will throw an error message. Because an integer parameter cannot be Null.

What is [BindProperty] attribute?

A BindProperty attribute can be applied to the public property of the controller or page model. When you apply this attribute, it will create a model binding to the property.

public class HomeController : Controller
{
[BindProperty]
public string Name { get; set; }
[BindProperty]
public string Age { get; set; }


}

The above one is an example of [BindProperty] attribute. The [BindProperty] applied to the public properties Name and Age. So it is a direction to the binding framework to bind the corresponding properties.

Also, you can use the [BindProperty] for complex type as shown below.

[BindProperty]
public Customer Customer { get; set; }

[BindProperty] properties

There are two properties available in [BindProperty] attribute, Name and SupportsGet. Usually, the model binding will connect the field names with the property. If the property does not match with any of the field names, then the property will not bind with any value. For example, the HTTP request is ~/Contact?usrID=5&name=harry, then the model binding will never bind the usrID value to ID property. In this case, the Name property helps to bind the value. The following is an example of how to bind the usrID value to ID property.

[BindProperty(Name =”usrID”)]
public string ID { get; set; }

By default, the model binding will work for the POST request. The SupportsGet property helps to bind the GET requests.

[BindProperty(SupportsGet = true)]

[BindProperties] attribute

The [BindProperties] attribute is available in ASP.NET core 2.1 and later. You can apply this attribute to the controller or page model class. So the model binding will bind all the public properties in the class.

[BindProperties]
public class ModelBindingModel : PageModel
{



}

From the above article, you can acquire knowledge about what is model binding in ASP.NET MVC, how it binds the request to a controller action, how it reduces the work of developers, what is [bindProperty] attributes and how to use the attribute with public properties. If you have any questions, please leave a comment.