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

Part 21 - Including and excluding properties from model binding using bind attribute

Suggested Videos 
Part 18 - Updating data
Part 19 - Unintended updates
Part 20 - Preventing unintended updates



In this video we will discuss, including and excluding properties from model binding using BIND attribute. Please watch Part 20, 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. There is another way to do the same thing using "Bind" attribute.



Modify "Edit_Post()" controller action method that is present in "EmployeeController.cs" file, as shown below.
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post([Bind(Include = "Id, Gender, City, DateOfBirth")] Employee employee)
{
    EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
    employee.Name = employeeBusinessLayer.Employees.Single(x => x.ID == employee.ID).Name;

    if (ModelState.IsValid)
    {
        employeeBusinessLayer.SaveEmployee(employee);

        return RedirectToAction("Index");
    }

    return View(employee);
}

Notice that, we are using "BIND" attribute and specifying the properties that we want to include in model binding. Since, "Name" property is not specified in the INCLUDE list, it will be excluded from model binding.
public ActionResult Edit_Post([Bind(Include = "Id, Gender, City, DateOfBirth")] Employee employee)

At this point, run the application and navigate to "http://localhost/MVCDemo/Employee/Edit/1". Click "Save" button, you will get a "Model" validation error stating - "The Name field is required".

This is because, we marked "Name" property in "Employee" class with "Required" attribute. Remove the "Required" attribute from "Name" property.
public class Employee
{
    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; }
}

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.

Alternatively, to exclude properties from binding, we can specify the exclude list using "Bind" attribute as shown below. 
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post([Bind(Exclude = "Name")] Employee employee)
{
    // Rest of the method implementation remains the same
}

9 comments:

  1. Hi Venkat,

    after when I remove the [Required] attribute on property Name the view Create don't show more the validation message if I try to create a User without the name. How can I solve this problem? I would to mantain the [Required] attribute.

    Thanks in advance
    Luca Jonathan Panetta

    ReplyDelete
    Replies
    1. Hi, in Part 28 of the asp.net mvc tutorial, we have discussed adding validation error messages dynamically, which will address the problem that you mentioned. Hope you will find it useful.

      Delete
  2. we should we remove [Required] for name

    employee.Name = employeeBusinessLayer.Employees.Single(x => x.ID == employee.ID).Name;

    if (ModelState.IsValid)
    {

    we are getting name value from employee.name then why we are getting modelstate is invalid

    ReplyDelete
    Replies
    1. You may have forgotten to remove Required attribute on Name property in Employee.CS

      Delete
  3. I think its because u have
    @Html.HiddenFor(model => model.Name)
    Where Name is passed in the hidden field

    ReplyDelete
  4. And now the question is if i am already passing the name using the hidden field via HTML Helper , then why is the model considered invalid ?

    ReplyDelete
  5. Venkat
    I'm a big fan. Do you have a video that discusses when to use which binding option? Maybe a best practices video? Even a series: I'd watch.

    Thanks much for all your efforts.

    Scott

    ReplyDelete
  6. problem stating that 'sequence contains no matching elements'

    ReplyDelete
  7. when i tried include and exclude, i encounterd a problem stating
    'sequence contains no matching elements', otherwise it is working fine.

    Can anyone tell me why is that.
    provide me solution.

    ReplyDelete

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