[index.cshtml]
@Html.EJS().Grid("Grid").ActionBegin("actionBegin").DataSource(dataManager =>{dataManager.Json(ViewBag.dataSource).InsertUrl("/Home/Insert").RemoveUrl("/Home/Delete").UpdateUrl("/Home/Update").Adaptor("RemoteSaveAdaptor");
}).EditSettings(edit =>
{
edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).ShowDeleteConfirmDialog(true)
.AllowEditOnDblClick(false).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog);
}).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Visible(false).IsIdentity(true).IsPrimaryKey(true).Width("30%").Add();
col.Field("EmployeeID").HeaderText("Employee ID").Width("150").Add();
col.Field("CustomerID").HeaderText("CustomerID").Width("70").Add();
}).AllowPaging().Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
</div>
<script>
var PkValue = 1000;
function actionBegin(args) {
if (args.requestType == 'save' && args.action == 'add') {
args.data[this.getPrimaryKeyFieldNames()[0]] = ++PkValue;
}
}
</script>
[controller]
public ActionResult Index()
{
ViewBag.dataSource = OrdersDetails.GetAllRecords().ToArray();
return View();
}
public ActionResult Update(OrdersDetails value)
{
var ord = value;
OrdersDetails val = OrdersDetails.GetAllRecords().Where(or => or.OrderID == ord.OrderID).FirstOrDefault();
val.OrderID = ord.OrderID;
val.EmployeeID = ord.EmployeeID;
val.CustomerID = ord.CustomerID;
return Json(value);
}
//insert the record
public ActionResult Insert(OrdersDetails value)
{
OrdersDetails.GetAllRecords().Insert(0, value);
return Json(new { data = value });
}
//Delete the record
public ActionResult Delete(int key)
{
OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == key).FirstOrDefault());
var data = OrdersDetails.GetAllRecords();
return Json(data);
}
. . .
}
|