Bind Attribute in ASP.NET MVC

Bind Attribute in ASP.NET MVC

In this article, I am going to discuss Bind Attribute in ASP.NET MVC Application to Include and Exclude properties in Model Binding. Please read our previous article where we discussed Unintended Updates in the ASP.NET MVC application. When we want to include or exclude properties from model binding then we need to use the Bind Attribute in ASP.NET MVC application. We are also going to work with the same example that we worked on in our previous article. As part of this article, we are going to discuss the following pointers.

  1. Understanding the Bind Attribute class.
  2. Including Model Properties using Bind Attributes.
  3. Excluding Model Properties using Bind Attribute.
Understanding the Bind Attribute in ASP.NET MVC:

Let us first have a look at the definition of the BindAttribute class in the ASP.NET MVC Framework. As you can see the BindAttribute class is inherited from the Attribute class and also contain some properties i.e. Exclude, Include, Prefix, and IsPropertyAllowed method.

Bind Attribute in ASP.NET MVC

  1. Exclude: The Exclude property is used to get or set a comma-delimited list of property names for which binding is not allowed.
  2. Include: The Include property is used to get or set a comma-delimited list of property names for which binding is allowed.
  3. Prefix: The Prefix property is used to gets or sets the prefix to use when markup is rendered for binding to an action argument or to a model property.
  4. IsPropertyAllowed: The IsPropertyAllowed method is used to determines whether the specified property is allowed. It returns true if the specified property is allowed; otherwise, false.
Bind Attribute Example in ASP.NET MVC Application:

Let us understand how to use the Bind Attribute in ASP.NET MVC Application with an example. First, modify the “Edit_Post()” action method of EmployeeController.cs as shown below.

[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post([Bind(Include = "Id, Gender, City, Salary, DateOfBirth")] Employee employee)
{
    EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
    employee.Name = employeeBusinessLayer.GetAllEmployess().FirstOrDefault(x => x.ID == employee.ID).Name;

    if (ModelState.IsValid)
    {
        employeeBusinessLayer.UpdateEmmployee(employee);
        return RedirectToAction("Index");
    }
    return View(employee);
}

As you can see in the above action code, using the “BIND” attribute we are specifying the properties that we want to include in our model binding using the Include property of BindAttribute class. As the “Name” property of the Employee model is not specified in the Include list, so it will be excluded from model binding. Now, if you generate a post request using fiddler as we did in the previous session, the “Name” property of the “Employee” object will not be updated.

Using the Exclude Property of BindAttribute class in ASP.NET MVC:

You can also achieve the same thing using the Exclude property of the Bind Attribute class in the ASP.NET MVC Application. Here, you need to specify the properties which you want to exclude from model binding as shown below in the below code.  

[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post([Bind(Exclude = "Name")] Employee employee)
{
    // Rest of the method implementation remains the same
}

As you can see in the above code, we specify the Name property in the Exclude list of the Bind Attribute class. So, here the Name property will be excluded from model binding.

In the next article, I am going to discuss how to include and exclude properties from Model Binding using Interface in ASP.NET MVC application. Here, in this article, I try to explain how to use the Bind Attribute to include and exclude properties from model binding in ASP.NET MVC Application with Examples. I hope you enjoy this Bind Attribute in the ASP.NET MVC article.

Leave a Reply

Your email address will not be published.