Model Binding Using Interface

Including and Excluding Properties from Model Binding Using Interface

In this article, I am going to discuss Model Binding using Interface in ASP.NET MVC Application. Please read our previous article before proceeding to this article where we discussed how to include and exclude model properties from model binding using Bind Attribute. This is also a continuation part of our previous article. Here, I will show you how to include and exclude properties in Model Binding using Interface.

Include and Exclude Properties from Model Binding using Interface

First Create an interface “IEmployee” as shown below.

Include and Exclude Properties from Model Binding using Interface

Notice that the above IEmployee interface has got only the properties that we want to include in model binding. The “Name” property is not present. This means “Name” property will be excluded from model binding. So, modify the “Employee.cs” class file in the “BusinessLayer” project as shown below.

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 the “Edit_Post()” action method of EmployeeController 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 you were generating 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 your project you need to choose the approach that best fits your needs. 

In the next article, I am going to discuss deleting the database record in ASP.NET MVC application. Here, 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. 

Leave a Reply

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