@(Html.EJS().Grid<mvc_ej2.Models.Order>("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor").InsertUrl("Home/Insert").UpdateUrl("/Home/Update").RemoveUrl("Home/Remove"); }).Width("auto").Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
. . .
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()) |
public class HomeController : Controller
{
public NORTHWNDEntities1 db = new NORTHWNDEntities1();
. . .
public ActionResult UrlDatasource(DataManagerRequest dm)
{
IEnumerable DataSource = OrderRepository.GetAllRecords();
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Order>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }, JsonRequestBehavior.AllowGet) : Json(DataSource);
}
public ActionResult Insert(Order value)
{
// here you can do your Insert Action
OrderRepository.Add(value);
return Json(value);
}
public ActionResult Remove(int key)
{
// here you can do your Delete Action
OrderRepository.Delete(key);
var data = OrderRepository.GetAllRecords();
var count = data.Cast<Order>().Count();
return Json(new { result = data, count = count });
}
public ActionResult Update(Order value)
{
var ord = value;
// here you can do your Update Action
Order val = OrderRepository.GetAllRecords().Where(or => or.OrderID == ord.OrderID).FirstOrDefault();
val.OrderID = ord.OrderID;
val.EmployeeID = ord.EmployeeID;
val.CustomerID = ord.CustomerID;
val.Freight = ord.Freight;
val.OrderDate = ord.OrderDate;
return Json(value);
}
}
|