DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Including and excluding properties from model binding using bind attribute

In ASP.NET MVC, you can include and exclude properties from model binding using the Bind attribute. The Bind attribute allows you to specify which properties should be included or excluded during model binding.

To include specific properties during model binding, you can use the Include property of the Bind attribute. Here's an example:

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; }
}
Enter fullscreen mode Exit fullscreen mode

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 Exclude property of the Bind attribute 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; }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, all properties except Address and Country will be included during model binding.

It's important to note that the Bind attribute is typically used with action methods in controllers to control model binding behavior. However, starting from ASP.NET Core 2.1, the [Bind] attribute is no longer necessary by default, as the framework uses a white-list approach for model binding security. This means that only properties explicitly specified in the action method parameters will be bound by default, and all other properties will be excluded.

Example in Action Method For Include

Certainly! Here's an example of using the Bind attribute in an action method of an ASP.NET MVC controller:

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult SaveData([Bind(Include = "Id,Name")] Mudassar model)
    {
        // Perform data saving logic using the included properties (Id and Name)
        // ...

        return RedirectToAction("Index");
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the SaveData action method is decorated with the [HttpPost] attribute to handle a POST request. The Mudassar model parameter is annotated with the [Bind(Include = "Id,Name")] attribute to include only the Id and Name properties during model binding.

When this action method is called, the ASP.NET MVC framework will bind the values from the HTTP request to the model parameter, but only the Id and Name properties will be populated. The Address and Country properties will be ignored.

You can further customize the Include and Exclude properties in the Bind attribute according to your specific requirements for model binding.

Example in Action Method For Include

Apologies for the confusion. The Bind attribute in ASP.NET MVC doesn't support an Exclude property. In earlier versions of ASP.NET MVC, there was an Exclude property available in the Bind attribute, but it was removed in ASP.NET Core.

In ASP.NET Core, the framework follows a "white-list" approach for model binding, meaning that only the properties explicitly specified in the action method parameters will be bound by default, and all other properties will be excluded. Therefore, there's no need to use the Bind attribute with an Exclude property in ASP.NET Core.

Here's an example of an action method where all properties except Address and Country will be excluded by default:

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult SaveData(Mudassar model)
    {
        // Perform data saving logic using the included properties (Id, Name)
        // ...

        return RedirectToAction("Index");
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the SaveData action method is decorated with the [HttpPost] attribute to handle a POST request. The Mudassar model parameter will only contain the properties explicitly sent in the request, in this case, Id, Name, and any other properties specified in the request.

Therefore, you don't need to use the Bind attribute with an Exclude property in ASP.NET Core, as the default behavior excludes all unspecified properties during model binding.

Top comments (5)

Collapse
 
bala_mcsd profile image
Balasubramanian R

What is purpose to exclude a property from model binding? When excluding some properties, the values populated for this properties are null and ModelState.IsValid is failing due to required attribute on those properties

Collapse
 
yuvan11 profile image
yuvaraj • Edited

Hi, Thanks for the post. How to apply the same feature of excluding fields in .NET core 6 Web API since [Bind(Exclude = "Address,Country")] is not supported. Thanks!

Collapse
 
yuvan11 profile image
yuvaraj

I need to exclude complex field type , I tried [BindNever] but it's not working.

Collapse
 
sardarmudassaralikhan profile image
Sardar Mudassar Ali Khan

In Asp.NET MVC this Feature is Available But For Asp.NET CORE This Feature is Not Available

Thread Thread
 
yuvan11 profile image
yuvaraj

Thanks for the reply. is there any recommended way or suggestion for Asp.NET CORE?