Editing Multiple Records Using Model Binding in MVC

Introduction

You saw that in ASP.NET MVC we can easily edit a single record handled by the default model binding. It may be possible that you need to edit multiple records. We can show an editable grid of data to the user and the user can easily edit multiple records. You can also save the edited data values by hitting a button.

In that context, multiple model objects are being submitted to the action method. The single record editing works on the assumption that form field names from the view match the corresponding model property names. However when multiple model objects are submitted this assumption is no longer true. So, in this article we'll create the MVC application in Visual Studio 2013.

So, let's start with the following sections:

  • Creating MVC application
  • Adding Model
  • Adding Controller
  • Adding View
  • Working the Controller and View

Creating MVC Application

In this section we'll create the MVC application using Visual Studio 2013 using the following procedure:

Step 1: Open Visual Studio 2013 and click on "New Project".

Step 2: Select the "ASP.NET Web Application" and enter the name as in the following:

Creating ASP.Net Web Application

Step 3: Select the "MVC" project template as in the following:

Mvc Project Template in VS 2013

Visual Studio automatically creates the MVC application using MVC 5 and adds files and folders to the project.

Adding Model

In this section we'll add a model for the application. We'll add the existing database to the model using an ADO.NET Entity Data Model using the following procedure.

Step 1: Add the ADO.NET Entity Data Model to the Model folder.

Step 2: Select the database and table as in the following:

Entity Data Model Wizard

After adding the data model you can see that the Employee entity is added to the application.

Employee Entity Diagram

Adding Controller

In this section, we'll add the controller for the model and view. Use the following procedure.

Step 1: Add a new Controller by right-clicking on the Controllers folder in the Solution Explorer.

Step 2: Select the "MVC 5 Controller - Empty" to generate the controller.

Creating Mvc Empty Controller

Step 3: Add the following code to the Index() method:

  1. using ModelBindingApp.Models;  
  2. public ActionResult Index()  
  3. {  
  4.     CompanyEntities DbCompany = new CompanyEntities();  
  5.     var data = from item in DbCompany.Employees  
  6.                 where item.Company == "TCS"  
  7.                 orderby item.ID  
  8.                 select item;  
  9.     return View(data.ToList());  
  10. } 

In the code above, this method selects employees from the TCS Company and passes them to the View as a list.

Adding View

In this section we'll add the view for the method in the controller. Use the following procedure.

Step 1: Right-click on the Index() to add the view as in the following:

Adding View in Mvc

Step 2: Replace the code with the code below:

  1. @model List<ModelBindingApp.Models.Employee>  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5. <h2>Employee List</h2>  
  6. @using (Html.BeginForm("Index""Sample", FormMethod.Post))  
  7. {  
  8.     <table class="table">  
  9.         @for (int i = 0; i < Model.Count; i++)  
  10.         {  
  11.             <tr>  
  12.                 <td>  
  13.                     @Html.TextBox("employees[" + @i + "].ID",  
  14.                     Model[i].ID, new {@readonly="readonly"})  
  15.                 </td>  
  16.                 <td>  
  17.                     @Html.TextBox("employees[" + @i + "].Name",   
  18.                     Model[i].Name, new { @readonly = "readonly" })  
  19.                 </td>  
  20.                 <td>  
  21.                     @Html.TextBox("employees[" + @i + "].Gender",   
  22.                     Model[i].Gender, new { @readonly = "readonly" })  
  23.                 </td>  
  24.                 <td>  
  25.                     @Html.TextBox("employees[" + @i + "].Company",   
  26.                     Model[i].Company, new { @readonly = "readonly" })  
  27.                 </td>  
  28.             </tr>  
  29.         }  
  30.         <tr>  
  31.             <td colspan="4">  
  32.                 <input type="submit" value="Submit" />  
  33.             </td>  
  34.         </tr>  
  35.     </table>  
  36. } 

In the code above, all the employee records will display having the company name TCS. You cannot edit the TextBox due to read-only. So, all the textboxes having the same index are considered as "one record".

Step 3: Press Ctrl+F5 to run the application.

Dispalyining Veiw of Controller in Mvc

Working the Controller and View

The data is submitted by the Index(). Now to handle the data we need to write the Index() method with a parameter. Use the following procedure.

Step 1: Add another Index() method with the following code:

  1. [HttpPost]  
  2. public ActionResult Index(List<Employee> employees)  
  3. {  
  4.     CompanyEntities DbCompany = new CompanyEntities();  
  5.     foreach (Employee Emp in employees)  
  6.     {  
  7.         Employee Existed_Emp = DbCompany.Employees.Find(Emp.ID);  
  8.         Existed_Emp.Name = Emp.Name;  
  9.         Existed_Emp.Gender = Emp.Gender;  
  10.         Existed_Emp.Company = Emp.Company;  
  11.     }  
  12.     DbCompany.SaveChanges();  
  13.     return View();  
  14. } 

In the code above, the Index() method takes the parameter named employees. Due to the naming conventions followed by the default binding in the MVC transforms the form fields value into a generic list of Employee objects. Once the changes have been made SaveChanges() is called to save the changes.

Step 2: We need to edit the markup code with the following code:

  1. @using (Html.BeginForm("Index""Sample", FormMethod.Post))  
  2. {  
  3.     <table class="table">  
  4.         @for (int i = 0; i < Model.Count; i++)  
  5.         {  
  6.             <tr>  
  7.                 <td>  
  8.                     @Html.Hidden("employees.Index", (@i+10))  
  9.                     @Html.TextBox("employees[" + (@i + 10) + "].ID",   
  10.                     Model[i].ID, new {@readonly="readonly"})  
  11.                 </td>  
  12.                 <td>  
  13.                     @Html.TextBox("employees[" + (@i + 10) + "].Name",   
  14.                     Model[i].Name)  
  15.                 </td>  
  16.                 <td>  
  17.                     @Html.TextBox("employees[" + (@i + 10) + "].Gender",  
  18.                     Model[i].Gender)                  
  19.                 </td>  
  20.                 <td>  
  21.                     @Html.TextBox("employees[" + (@i + 10) + "].Company",  
  22.                     Model[i].Company)                  
  23.                 </td>  
  24.             </tr>  
  25.         }  
  26.         <tr>  
  27.             <td colspan="4">  
  28.                 <input type="submit" value="Submit" />  
  29.             </td>  
  30.         </tr>  
  31.     </table>  
  32. } 

In the code above, each table row has the hidden field named Index and the value is set to some arbitrary value. All textboxes have the same index that is specified by the hidden field and considered as "one record".

Step 3: Now again run the application and edit multiple records.

Editing Multiple Objects

Step 4: You can see the records have been updated as in the following:

Generating Index View

Summary

This article described how to update multiple records at a time in an ASP.NET MVC application. Thanks for reading.