Loading

ASP.NET MVC

What is Model Binding Using Interface?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

In this Video, I am going to discuss Including and Excluding Properties from Model Binding using Interface in ASP.NET MVC Application. Please read our previous Video before proceeding to this Video where we discussed How to include and Exclude Properties using Bind Attributein ASP.NET MVC Application. This is also a continuation part of our previous Video. 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 in ASP.NET MVC Application. Depending on the architecture and requirements of your project you need to choose the approach that best fits your needs. 

See All

Comments (286 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /286

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /286

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /286