how to return data from server while performing CRUD operation if IsIdentity column is defined
When enabling the IsIdentity property for Auto incremented column then we should return the values along with the response which is generated the value for IsIdentity column. Please refer to the following code example,
JS
var dataManager = ej.DataManager({ url: "/Home/DataSource", insertUrl: "/Home/Insert", updateUrl: "/Home/Update", DeleteUrl: "/Home/Delete", adaptor:"UrlAdaptor"});
$("#Grid").ejGrid({ dataSource: dataManager, allowPaging: true, editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true }, toolbarSettings:{showToolbar:true,toolbarItems:["add","edit","cancel","update","delete"]}, columns: [ { field: "OrderID", isPrimaryKey: true, isIdentity:true, type:"number", headerText: "Order ID" }, { field: "EmployeeID", type:"number", headerText: 'Employee ID' }, { field: "Freight", type:"number", headerText: 'Freight', format: "{0:C}" }, { field: "CustomerID",type:"string",headerText: 'Ship City' },
],
}); |
MVC
@(Html.EJ().Grid<EditableOrder>("Grid") .Datasource(ds => ds.URL(@Url.Action("DataSource")).UpdateURL("/Home/Update").InsertURL("/Home/Insert").RemoveURL("/Home/Delete") .Adaptor(AdaptorType.UrlAdaptor)) .AllowPaging() .Columns(col => {
col.Field(p => p.OrderID).IsPrimaryKey(true).IsIdentity(true).HeaderText("Order ID").Add(); col.Field(p => p.EmployeeID).HeaderText("Employee ID").Add(); col.Field(p => p.Freight).HeaderText("Freight").Format("{0:C2}").Add(); col.Field(p => p.CustomerID).HeaderText("Customer ID").TextAlign(TextAlign.Left).Add(); }) )
|
Server Side
public ActionResult Insert(EditableOrder value) { OrderRepository.Add(value); var data = OrderRepository.GetAllRecords(); return Json(value, JsonRequestBehavior.AllowGet); } |
Note: Data source will be handled the Auto incremented column. Otherwise we need to set the value for IsIdentity column like as follows.
public ActionResult Insert(EditableOrder value) {
var data = OrderRepository.GetAllRecords().Last(); value.OrderID = ++data.OrderID; OrderRepository.Add(value); return Json(value, JsonRequestBehavior.AllowGet); } |