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. Please read our previous article where we discussed Unintended Updates in ASP.NET MVC application. When you want to include or exclude properties from model binding then you need to use the Bind Attribute in ASP.NET MVC application. We are also going to work with the same example that we worked 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 Attribute.
  3. Excluding Model Properties using Bind Attribute.
Understanding the Bind Attribute in ASP.NET MVC:

Let us have a look at the definition of the BindAttribute class.

Bind Attribute in ASP.NET MVC

Using the Exclude property we can get or set a comma-delimited list of property names for which binding is not allowed. On the other hand, using the Include property we can get or set a comma-delimited list of property names for which binding is allowed. The Prefix property is basically used to gets or sets the prefix to use when markup is rendered for binding to an action argument or to a model property.

Let us understand the use of Bind Attribute 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().Single(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 method, using the “BIND” attribute we are specifying the properties that we want to include in model binding using the Include property of BindAttribute class. As the “Name” property is not specified in the Include list, so it will be excluded from model binding.

Now, if you were to 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:

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

[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 Bind Attribute. So, here except 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 step by step with a simple example.

Leave a Reply

Your email address will not be published. Required fields are marked *