Grid:
<ejs-grid id="Grid" allowPaging="true" toolbar="@(new List<string>() {"Add", "Edit", "Update", "Delete" })">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor" insertUrl="/Home/Insert" , updateUrl="/Home/Update" removeUrl="/Home/Remove"></e-data-manager>
...
</ejs-grid>
Controller:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2Grid.Models;
using Syncfusion.EJ2.Base;
using System.Collections;
namespace EJ2Grid.Controllers
{
public class HomeController : Controller
{
public static List<Orders> order = new List<Orders>();
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = order;
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<Orders>().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 IActionResult Insert([FromBody]CRUDModel<Orders> value) // Insert the new record
{
order.Insert(0, value.Value);
return Json(value.Value);
}
public IActionResult Update([FromBody]CRUDModel<Orders> value) // Update record
{
var data = order.Where(or => or.OrderID == value.Value.OrderID).FirstOrDefault();
if(data != null)
{
data.OrderID = value.Value.OrderID;
data.CustomerID = value.Value.CustomerID;
data.EmployeeID = value.Value.EmployeeID;
data.OrderDate = value.Value.OrderDate;
data.ShipCity = value.Value.ShipCity;
data.Freight = value.Value.Freight;
}
return Json(value.Value);
}
public void Remove([FromBody]CRUDModel<Orders> Value) // Remove record
{
var data = order.Where(or => or.OrderID.Equals(Value.Key)).FirstOrDefault();
order.Remove(data);
}
}
} |
<ejs-grid id="Grid" allowPaging="true" toolbar="@(new List<string>() {"Add", "Edit", "Update", "Delete" })">
<e-data-manager json="@ViewBag.data" adaptor="RemoteSaveAdaptor" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Remove"></e-data-manager>
...
</ejs-grid>
Controller:
namespace EJ2Grid.Controllers
{
public class HomeController : Controller
{
public static List<Orders> order = new List<Orders>();
public IActionResult Index()
{
if (order.Count == 0)
BindDataSource();
ViewBag.data = order.ToArray();
return View();
}
public IActionResult Insert([FromBody]CRUDModel<Orders> value)
{
order.Insert(0, value.Value);
return Json(value.Value);
}
public IActionResult Update([FromBody]CRUDModel<Orders> value)
{
var data = order.Where(or => or.OrderID == value.Value.OrderID).FirstOrDefault();
if(data != null)
{
data.OrderID = value.Value.OrderID;
data.CustomerID = value.Value.CustomerID;
data.EmployeeID = value.Value.EmployeeID;
data.OrderDate = value.Value.OrderDate;
data.ShipCity = value.Value.ShipCity;
data.Freight = value.Value.Freight;
}
return Json(value.Value);
}
public void Remove([FromBody]CRUDModel<Orders> Value)
{
var data = order.Where(or => or.OrderID.Equals(Value.Key)).FirstOrDefault();
order.Remove(data);
}
}
} |
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
Options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(“TEST”);
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
} |