Model Binding Using Interface

Including and excluding properties from Model Binding Using Interface

In this article, we will discuss include and exclude model properties from model binding using interface in ASP.NET MVC application. Please read our last article before proceeding to this article where I discuss how to use Bind attribute to include and exclude properties in model binding. 

In Unintended Update Article, we have seen how to include and exclude properties from model binding by passing a string array to UpdateModel() method and in our last article, we have seen achieving the same using “BIND” attribute. 

To include and exclude properties from model binding using interfaces

First Create an interface “IEmployee” as shown below. Notice that this interface has got only the properties that we want to include in model binding. “Name” property is not present. This means “Name” property will be excluded from model binding. Copy and paste this code in “Employee.cs” class file in “BusinessLayer” project.

namespace BusinessLayer
{
    public interface IEmployee
    {
        int ID { get; set; }
        string Gender { get; set; }
        string City { get; set; }
        decimal Salary { get; set; }
        DateTime DateOfBirth { get; set; }
    }

// Step 2: Make "Employee" class inherit from IEmployee interface
    public class Employee : IEmployee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
        public decimal Salary { get; set; }
        public DateTime DateOfBirth { get; set; }
    }
}

Next, Modify “Edit_Post()” controller action method that is present in “EmployeeController.cs” file as shown below.

[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(int id)
{
    EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
    Employee employee = employeeBusinessLayer.GetAllEmployess().Single(x => x.ID == id);
    UpdateModel<IEmployee>(employee);
    if (ModelState.IsValid)
    {
        employeeBusinessLayer.UpdateEmmployee(employee);
        return RedirectToAction("Index");
    }
    return View(employee);
}

Notice that we are explicitly calling the model binder by calling UpdateModel() function passing our interface IEmployee. The model binder will update only the properties that are present in the interface.

So if we were to generate a post request using fiddler as we did in the previous session “Name” property of the “Employee” object will not be updated.

So, in short, there are several ways to include and exclude properties from Model Binding. Depending on the architecture and requirements of our project we may choose the approach that best fit our needs. 

In the next article, I will explain deleting the database record in ASP.NET MVC application

SUMMARY

In this article, I try to explain how to use interface to include and exclude properties from model binding in ASP.NET MVC application step by step with a simple example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *