How to save data in server while batch editing is enabled
Essential JavaScript Grid allows option to perform server side batch editing. This can be enabled by using the UrlAdaptor/RemoteSaveAdaptor in grid datasource and by specifying the URL to the batch operation. While performing batch editing, the needed information will be send to server through as AJAX post request.
In the following code snippet, we have demonstrated batch editing operation using UrlAdaptor.
Grid Initialization
JS
<div id="Grid"></div> <script type="text/javascript"> $(function () { $("#Grid").ejGrid({ dataSource: ej.DataManager({ url: "/Home/BatchData", batchUrl: "/Home/BatchUpdate" , adaptor: "UrlAdaptor" }), allowPaging: true, editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: "batch" }, toolbarSettings: { showToolbar: true, toolbarItems: ["add", "edit", "delete", "update", "cancel"] }, columns: [{ field: "Id", isPrimaryKey: true }, { field: "CompanyName" }, { field: "City" }, { field: "PostalCode" }, { field: "MobileNumber" }] }); }); </script>
MVC
@(Html.EJ().Grid<object>("ExportGrid") .Datasource(ds => { ds.URL("/Home/BatchData").BatchURL("/Home/BatchUpdate").Adaptor(AdaptorType.UrlAdaptor);}) .AllowPaging() .EditSettings(e => e.AllowEditing().AllowDeleting().AllowAdding().EditMode(EditMode.Batch)) .ToolbarSettings(tools => tools.ShowToolbar().ToolbarItems(item => { item.AddTool(ToolBarItems.Add); item.AddTool(ToolBarItems.Edit); item.AddTool(ToolBarItems.Delete); item.AddTool(ToolBarItems.Update); item.AddTool(ToolBarItems.Cancel); })) .Columns(col => { col.Field("Id").IsPrimaryKey(true).Add(); col.Field("CompanyName").Add(); col.Field("City").Add(); col.Field("PostalCode").Add(); col.Field("MobileNumber").Add(); }))
The action in which, the batch save operation is performed is as follows.
public ActionResult BatchUpdate(string key, List<Supplier> changed, List<Supplier> added, List<Supplier> deleted) { NORTHWND1Entities db = new NORTHWND1Entities(); //Performing insert operation if (added != null && added.Count() > 0) { foreach (var temp in added) { db.Suppliers.Add(temp); } } ////Performing update operation if (changed != null && changed.Count() > 0) { foreach (var temp in changed) { Supplier old = db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault(); if (old != null) { db.Entry(old).CurrentValues.SetValues(temp); } } } //Performing delete operation if (deleted != null && deleted.Count() > 0) { foreach (var temp in deleted) { db.Suppliers.Remove(db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault()); } } db.SaveChanges(); return RedirectToAction("BatchData"); }
ASPX
<ej:Grid ID="Grid" runat="server" AllowPaging="true"> <DataManager URL="Default.aspx/BatchData" BatchURL="Default.aspx/BatchUpdate" Adaptor="UrlAdaptor" /> <EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" EditMode="Batch" /> <ToolbarSettings ShowToolbar="true" ToolbarItems="add,edit,delete,update,cancel"/> <Columns> <ej:Column Field="Id" IsPrimaryKey="true" IsIdentity="true"/> <ej:Column Field="CompanyName"/> <ej:Column Field="City"/> <ej:Column Field="PostalCode"/> <ej:Column Field="MobileNumber"/> <ej:Column Field="EmailAddress"/> <ej:Column Field="WebAddress"/> <ej:Column Field="IsActive"/> </Columns> </ej:Grid>
In Webforms, the batch operation can performed in the Webmethod, please refer the below code snippet.
//Webmethod called when batch save operation performed [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static void BatchUpdate(string key, List<Supplier> changed, List<Supplier> added, List<Supplier> deleted) { NORTHWND1Entities db = new NORTHWND1Entities(); //Performing insert operation if (added != null && added.Count() > 0) { foreach (var temp in added) { db.Suppliers.Add(temp); } } ////Performing update operation if (changed != null && changed.Count() > 0) { foreach (var temp in changed) { Supplier old = db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault(); if (old != null) { db.Entry(old).CurrentValues.SetValues(temp); } } } //Performing delete operation if (deleted != null && deleted.Count() > 0) { foreach (var temp in deleted) { db.Suppliers.Remove(db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault()); } } db.SaveChanges(); }
Result
Figure 1: Grid with batch editing
The POST request and response to the server during the batch processing will be as follows.
Figure 2: Batch request send to the server with edited data information on save operation.