Including And Excluding Properties from Model Binding Using Interfaces

Model binding using interfaces

You can specify distinct interfaces for various sets of characteristics and have your model implement those interfaces if you want to include or omit certain properties from model binding. After that, you can bind your model to that interface type to decide which properties are added when the model is bound.

Here’s an example of how you can achieve this.

The interfaces “IIncludeProperties” and “IExcludeProperties” are defined in the example above. When modeling binding, each interface represents a set of properties that you want to include or exclude. Both interfaces are implemented by the class “Mudassar”.

You may now link your model to the specified interface type in your action method to include or exclude properties.

The [HttpPost] element is used in the example above to decorate the “SaveData” action method so that it may handle a POST request. The [Bind(typeof(IIncludeProperties))] attribute on the “Mudassar” model parameter indicates that only the properties described in the “IIncludeProperties” interface (in this case, “Id” and “Name”) should be included during model binding. The properties “Address” and “Country” will be disregarded.

You can choose which set of attributes are included during model binding in various action methods by specifying various interface types in the ‘Bind’ property.

Select the Top ASP.NET Website Hosting

HostForLIFE.eu  is recognized as one of the Best, Cheap WordPress Web Hosting Providers in UK. You can always start from their start from €3.49/month and this plan has supported WordPress, Joomla, Drupal, Umbraco, etc, with a one-click installer within less than 5 minutes. They provide cheap, best and instant activation on your WordPress hosting account with UNLIMITED Bandwidth, disk space and Domain.

You may also like...

Popular Posts