Bind Attribute in ASP.NET MVC
Using the 'Bind' Attribute in ASP.NET MVC, you can include and exclude properties from model binding. You can specify which attributes should be included or excluded during model binding using the 'Bind' feature.
To include particular characteristics during model binding, utilize the 'Include' property of the 'Bind' attribute. Here is one instance.
public class Mudassar
{
[Bind(Include = Id,Name)]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
In the above example, only the `Id` and `Name` properties will be included during model binding. The `Address` and `Country` properties will be excluded.
Similarly, you can use the `Bind' attributes Exclude' property to exclude specific properties from model binding. Here's an example.
public class Mudassar
{
[Bind(Exclude = Address,Country)]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
All the properties in the example—aside from "Address" and "Country"—will be included during model binding.
The 'Bind' attribute is frequently used by controllers with action methods to regulate model binding behavior, which is why it's crucial to keep this in mind. The "[Bind]" attribute is no longer required by default in ASP.NET Core 2.1, as the framework now employs a white-list strategy for model binding security. This indicates that all other properties will be excluded by default and that only those properties that are expressly mentioned in the action method arguments will be bound.
Example in Action Method For Include
public class HomeController : Controller
{
[HttpPost]
public ActionResult SaveData([Bind(Include = "Id,Name")] Mudassar model)
{
return RedirectToAction("Index");
}
}
The [HttpPost] element is used in the example above to decorate the "SaveData" action method so that it may handle a POST request. The model parameter "Mudassar" has the annotation "[Bind (Include = "Id, Name")" to limit the inclusion of model binding to just the "Id" and "Name" values.
The ASP.NET MVC framework will bind the values from the HTTP request to the model parameter when this action method is invoked. However, only the 'Id' and 'Name' attributes will be filled. The "Address" and "Country" properties won't be considered.
According to your unique model binding needs, you can further configure the "Include" and "Exclude" properties in the "Bind" property.
Example in Action Method for Exclude
In ASP.NET MVC, the 'Bind' attribute does not support a 'Exclude' property. The "Exclude" property was present in the "Bind" attribute of older iterations of ASP.NET MVC, but it was eliminated in ASP.NET Core.
Model binding in ASP.NET Core uses a "whitelist" strategy, whereby only attributes that are explicitly mentioned in the action method arguments are bound by default, and all other properties are not. Therefore, in ASP.NET Core, pairing a "Exclude" property with the "Bind" attribute is unnecessary.
Here is an illustration of an action method where all properties are by default excluded, apart from "Address" and "Country":
public class HomeController : Controller
{
[HttpPost]
public ActionResult SaveData(Mudassar model)
{
return RedirectToAction("Index");
}
}
The [HttpPost] element is used to decorate the "SaveData" action method in the example above so that it may handle a POST request. The "Mudassar" model parameter will only include the explicit request properties, in this case, "Id," "Name," and any other request-specific properties.
Because ASP.NET Core excludes all unnamed attributes by default during model binding, you don't need to utilize the 'Bind' attribute with a 'Exclude' property.