Including and excluding properties from model binding using bind attribute
Tags: webdev, programming, api, aspdotnet
Published: 2023-07-05 18:47:48

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:

```csharp
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 `Exclude` property of the `Bind` attribute to exclude specific properties from model binding. Here's an example:

```csharp
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; }
}
```

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:

```csharp
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");
    }
}
```

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:

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

        return RedirectToAction("Index");
    }
}
```

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.