Loading

ASP.NET MVC

How to Bind Attribute in ASP.NET MVC?. The Complete ASP.NET MVC Developer Course 2022 [Videos].

In this Video, I am going to discuss Bind Attribute in ASP.NET MVC Application to Include and Exclude properties in Model Binding. Please watch our previous Video 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 Video. As part of this Video, 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.

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.

See All

Comments (285 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /285

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /285

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /285