October 7, 2014 12:11 by
Peter
In this post we are going to update multiple row using ASP.NET MVC and Entity Framework. Just follow these steps below:
1. First we need to create a project.
Go to Menu File > New > Project > Select ASP.NET MVC web application > Entry Application Name > Click OK.
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.
Open Database and add a table for update operation. Here I am creating a table called Contacts.
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.
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.
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 fileOpen file and modify as for enable validation.
namespace UpdateMultiRecord
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Contac
{
[Required]
public int ContactID { get; set; }
[Required]
public string ContactPerson { get; set; }
[Required]
public string Contactno { get; set; }
public string EmailID { get; set; }
}
}
Here I am using Home controller index action.
Get Action
[HttpGet]
public ActionResult Index()
{
List<Contact> model = new List<Contact>();
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
model = dc.Contacts.ToList();
}
return View(model);
}
Post Action
[HttpPost]
public ActionResult Index(List<Contact> list)
{
if (ModelState.IsValid)
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
foreach (var i in list)
{
var c = dc.Contacts.Where(a =>
a.ContactID.Equals(i.ContactID)).FirstOrDefault();
if (c != null)
{
c.ContactPerson = i.ContactPerson;
c.Contactno = i.Contactno;
c.EmailID = i.EmailID;
}
}
dc.SaveChanges();
}
ViewBag.Message = "Successfully Updated.";
return View(list);
}
else
{
ViewBag.Message = "Failed ! Please try again.";
return View(list);
}
}
Create View for Update Multiple Row.
@model List<UpdateMultiRecord.Contact>
@{
ViewBag.Title = "Update multiple row at once Using MVC 4 and EF ";
}
@using (@Html.BeginForm("Index","Home", FormMethod.Post))
{
<table>
<tr>
<th></th>
<th>Contact Person</th>
<th>Contact No</th>
<th>Email ID</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td> @Html.HiddenFor(model => model[i].ContactID)</td>
<td>@Html.EditorFor(model => model[i].ContactPerson)</td>
<td>@Html.EditorFor(model => model[i].Contactno)</td>
<td>@Html.EditorFor(model => model[i].EmailID)</td>
</tr>
}
</table>
<p><input type="submit" value="Save" /></p>
<p style="color:green; font-size:12px;">
@ViewBag.Message
</p>
}
@section Scripts
{@Scripts.Render("~/bundles/jqueryval")}
Code: @Scripts.Render("~/bundles/jqueryval") will enable client side validation. Finally, Run Application. Edit Contact Details and Click Save button.