@Html.EJS().Grid("Grid").DataSource(ds => ds.Url("/Home/UrlDataSource").UpdateUrl("/Home/Update").InsertUrl("/Home/Insert").RemoveUrl("/Home/Remove").Adaptor("UrlAdaptor")).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
. . .
}).Query("new ej.data.Query().addParams('ej2Grid', 'true')").AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
|
namespace mvc_ej2.Controllers
{
public class HomeController : Controller
{
public ActionResult UrlDatasource(TestDm dm)
{
IEnumerable DataSource = orddata.ToList();
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<OrdersDetails>().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 }) : Json(DataSource);
}
public ActionResult Update(CRUDModel myObject)
{
var ord = myObject.Value;
OrdersDetails val = orddata.Where(or => or.OrderID == ord.OrderID).FirstOrDefault();
. . .
return Json(myObject.Value);
}
public ActionResult Insert(CRUDModel myObject)
{
var ord = myObject.Value;
orddata.Insert(0, ord);
return Json(myObject.Value);
}
public ActionResult Remove(CRUDModel myObject)
{
orddata.Remove(orddata.Where(or => or.OrderID == myObject.key).FirstOrDefault());
return Json(new { result = orddata, count = orddata.Count });
}
public class TestDm : DataManagerRequest
{
public string ej2Grid { get; set; }
}
public class CRUDModel
{
public List<OrdersDetails> Added { get; set; }
public List<OrdersDetails> Changed { get; set; }
public List<OrdersDetails> Deleted { get; set; }
public OrdersDetails Value { get; set; }
public int key { get; set; }
public string action { get; set; }
public string ej2Grid { get; set; }
}
} |