Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 22 - Including and excluding properties from model binding using interfaces

Suggested Videos 
Part 19 - Unintended updates
Part 20 - Preventing unintended updates
Part 21 - Including and excluding properties from model binding using bind attribute



In this video we will discuss, including and excluding properties from model binding using interfaces. Please watch Part 21, before proceeding. 

In part 20, we have seen how to include and exclude properties from model binding, by passing a string array to UpdateModel() method, and in part 21 we have seen achieving the same using "BIND" attribute.



To include and exclude properties from model binding using interfaces
Step 1: 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
public interface IEmployee
{
    int ID { get; set; }
    string Gender { get; set; }
    string City { 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; }
    [Required]
    public string Gender { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public DateTime? DateOfBirth { get; set; }
}

Step 3: 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.Employees.Single(x => x.ID == id);
    UpdateModel<IEmployee>(employee);

    if (ModelState.IsValid)
    {
        employeeBusinessLayer.SaveEmmployee(employee);
        return RedirectToAction("Index");
    }

    return View(employee);
}

Notice that we are explicitly calling the model binder, by calling UpdateModel() function passing in 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 your project, you may choose the approach that best fit your needs.

6 comments:

  1. Hi,
    I am Ashish. In this Example you used class Employee to update records. In Employee class you removed attribute [Required] on "Name" property. In Case of adding new record we want this attribute on "Name" property. So in that case for every Add and update i have separate Employee class? How To handle this situation.

    ReplyDelete
    Replies
    1. Hi Ashish, this is a very good question. We discussed adding Model validation errors dynamically in Part 28 of the asp.net mvc video tutorial.

      Delete
    2. Hi,
      I saw Part 28, in this part validation happen at server side and MVC support client side validation as well. So in that case, for adding a new record i also required "[Required]" attribute on "Name" property for client side validations.So my question remains as it is.
      How to handle this situation?

      Delete
    3. I am using Required Attribute in employee Class it is working while using Interface Method for including and Ecluding Properties..

      Delete
  2. I thought interface cannot have fields and only methods. why do the interface have fields here?

    ReplyDelete

It would be great if you can help share these free resources