Saturday, August 10, 2019

How to update multiple row at once Using MVC 4 and EF (Entity framework)

In one of my article, I have shown you how to insert multiple rows to a database using asp.net MVC. but recently I came across a scenario when I need to implement a bulk update operation through a Web interface. Here In this article, I have explained how to update multiple rows at once using entity framework in asp.net MVC application.

If you have asp.net webforms project and using stored procedure, please visit how to update bulk data (multiple rows) to a SQL server database in asp.net webform.  and for insert multiple rows at once in asp.net webforms visit how to update bulk data (multiple rows) to a SQL Server database using ASP.NET

Prerequisite

I used followings:
  • .Net framework 4.0
  • Entity Framework
  • Jquery
  • Sql Server 2008

Steps

Just follow the steps and get result easily.

Step-1: Create New Project

First we need to create a project.
Go to Menu File > New > Project > select asp.net mvc4 web application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.
Add Database
Open Database and add a table for update operation. Here I am creating a table called Contacts.

Step-3: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project Name from Solution Explorer folder > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
Add Entity Model
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next > Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.
GenerateFromDatabase
After Creating Data model, we have to modify our generated entity(table) for Apply validation for required fields.
Here we need to modify contact.cs file
Contact.CS
Open file and modify as for enable validation.
  1. namespace UpdateMultiRecord
  2. {
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.DataAnnotations;
  7.  
  8. public partial class Contact
  9. {
  10. [Required]
  11. public int ContactID { get; set; }
  12. [Required]
  13. public string ContactPerson { get; set; }
  14. [Required]
  15. public string Contactno { get; set; }
  16. public string EmailID { get; set; }
  17. }
  18. }

Step-4: Implement Action for Get & Post Method.

Here I am using Home controller index action.
Get Action 
  1. [HttpGet]
  2. public ActionResult Index()
  3. {
  4. List<Contact> model = new List<Contact>();
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. model = dc.Contacts.ToList();
  8. }
  9. return View(model);
  10. }
Post Action 
  1. [HttpPost]
  2. public ActionResult Index(List<Contact> list)
  3. {
  4. if (ModelState.IsValid)
  5. {
  6. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  7. {
  8. foreach (var i in list)
  9. {
  10. var c = dc.Contacts.Where(a =>a.ContactID.Equals(i.ContactID)).FirstOrDefault();
  11. if (c != null)
  12. {
  13. c.ContactPerson = i.ContactPerson;
  14. c.Contactno = i.Contactno;
  15. c.EmailID = i.EmailID;
  16. }
  17. }
  18. dc.SaveChanges();
  19. }
  20. ViewBag.Message = "Successfully Updated.";
  21. return View(list);
  22. }
  23. else
  24. {
  25. ViewBag.Message = "Failed ! Please try again.";
  26. return View(list);
  27. }
  28. }

Step-5: Create View for Update Multiple Row.

Our View Index.cshtml
  1. @model List<UpdateMultiRecord.Contact>
  2. @{
  3. ViewBag.Title = "Update multiple row at once Using MVC 4 and EF ";
  4. }
  5. @using (@Html.BeginForm("Index","Home", FormMethod.Post))
  6. {
  7. <table>
  8. <tr>
  9. <th></th>
  10. <th>Contact Person</th>
  11. <th>Contact No</th>
  12. <th>Email ID</th>
  13. </tr>
  14. @for (int i = 0; i < Model.Count; i++)
  15. {
  16. <tr>
  17. <td> @Html.HiddenFor(model => model[i].ContactID)</td>
  18. <td>@Html.EditorFor(model => model[i].ContactPerson)</td>
  19. <td>@Html.EditorFor(model => model[i].Contactno)</td>
  20. <td>@Html.EditorFor(model => model[i].EmailID)</td>
  21. </tr>
  22. }
  23. </table>
  24. <p><input type="submit" value="Save" /></p>
  25. <p style="color:green; font-size:12px;">
  26. @ViewBag.Message
  27. </p>
  28. }
  29. @section Scripts{
  30. @Scripts.Render("~/bundles/jqueryval")
  31. }


Here
  1. @Scripts.Render("~/bundles/jqueryval")
 this will enable client side validation.

Step-6: Run Application.

Edit Contact Details and Click Save button.
Browser
Show message Successfully Updated.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...