|
@{
<ejs-grid id="cruiseLinesGrid" dataSource=@Model.Datasource toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" allowPaging="true">
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
<e-grid-columns>
<e-grid-column field="EmployeeID" headerText="Employee ID" width="125"></e-grid-column>
<e-grid-column field="OrderID" headerText="OrderID" width="120"></e-grid-column>
. . . .
</e-grid-columns>
</ejs-grid>
} |
|
<ejs-grid id="Grid" allowPaging="true" load="onLoad" toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})">
<e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editsettings>
<e-data-manager url="/Index?handler=DataSource" insertUrl="/Index?handler=Insert" updateUrl="/Index?handler=Update" removeUrl="/Index?handler=Delete" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid> |
|
public class IndexModel : PageModel
{
public void OnGet()
{
}
public JsonResult OnPostDataSource([FromBody]Data dm)
{
var data = OrdersDetails.GetAllRecords();
int count = data.Cast<OrdersDetails>().Count();
return dm.requiresCounts ? new JsonResult(new { result = data.Skip(dm.skip).Take(dm.take), count = count }) : new JsonResult(data);
}
public JsonResult OnPostInsert([FromBody]CRUDModel<OrdersDetails> value)
{
OrdersDetails.GetAllRecords().Insert(0, value.value);
return new JsonResult(value.value);
}
public JsonResult OnPostDelete([FromBody]CRUDModel<OrdersDetails> value)
{
OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == int.Parse(value.key.ToString())).FirstOrDefault());
return new JsonResult(value);
}
|
|
<ej-grid id="FlatGrid" allow-sorting="true" selectiontype="Multiple"allow-paging="true">
<e-datamanager url="/Demo?handler=DataSource" update-url="/Demo?hanlder=Update" insert-url="/Demo?hanlder=Insert" remove-url="/Demo?hanlder=Delete" adaptor="UrlAdaptor"></e-datamanager>
<e-columns>
<e-column field="OrderID" header-text="Order ID" text-align="Right" width="75" is-primary-key="true"></e-column>
<e-column field="CustomerID" header-text="Customer ID" width="80"></e-column>
<e-column field="EmployeeID" header-text="Employee ID" text-align="Left" width="75"></e-column>
</e-columns>
</ej-grid>
</div>
pageModel
namespace ASPNetRazorPageDemo.Pages
{
public class DemoModel : PageModel
{
public static List<Orders> order = new List<Orders>();
public void BindDataSource()
{
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new Orders(code + 1, "ALFKI", i + 0, 2.3 * i, new DateTime(1991, 05, 15), "Berlin"));
order.Add(new Orders(code + 2, "ANATR", i + 2, 3.3 * i, new DateTime(1990, 04, 04), "Madrid"));
order.Add(new Orders(code + 3, "ANTON", i + 1, 4.3 * i, new DateTime(1957, 11, 30), "Cholchester"));
order.Add(new Orders(code + 4, "BLONP", i + 3, 5.3 * i, new DateTime(1930, 10, 22), "Marseille"));
order.Add(new Orders(code + 5, "BOLID", i + 4, 6.3 * i, new DateTime(1953, 02, 18), "Tsawassen"));
code += 5;
}
}
public JsonResult OnPostDataSource([FromBody]DataManager dm)
{
if (order.Count == 0)
BindDataSource();
IEnumerable data = order;
DataOperations operation = new DataOperations();
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
data = operation.PerformSorting(data, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator);
}
if (dm.Search != null && dm.Search.Count > 0) //Searching
{
data = operation.PerformSearching(data, dm.Search);
}
var count = data.AsQueryable().Count();
if (dm.Skip != 0)
{
data = operation.PerformSkip(data, dm.Skip);
}
if (dm.Take != 0)
{
data = operation.PerformTake(data, dm.Take);
}
return new JsonResult(new { result = data, count = count });
}
public class Orders
{
. . .
}
public JsonResult OnPostUpdate([FromBody]CRUDModel<Orders> value)
{
var ord = value.value;
Orders val = order.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;
val.ShipCity = ord.ShipCity;
return new JsonResult(value.value);
}
//insert the record
public JsonResult OnPostInsert([FromBody]CRUDModel<Orders> value)
{
order.Insert(0, value.value);
return new JsonResult(value);
}
//Delete the record
public JsonResult OnPostDelete([FromBody]CRUDModel<Orders> value)
{
order.Remove(order.Where(or => or.OrderID == int.Parse(value.key.ToString())).FirstOrDefault());
return new JsonResult(value);
}
}
}
|
Hi Patricio,In the previously provided sample we are bind local data to the Grid so that it update values in grid level .Now we have modified the sample and bind remote data to the Grid also performed CRUD operation in Grid. Kindly refer to the below code example and updated sample for more information.
[index.cshtml]
<ejs-grid id="Grid" allowPaging="true" load="onLoad" toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})"><e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editsettings><e-data-manager url="/Index?handler=DataSource" insertUrl="/Index?handler=Insert" updateUrl="/Index?handler=Update" removeUrl="/Index?handler=Delete" adaptor="UrlAdaptor"></e-data-manager><e-grid-columns><e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column><e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column><e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column></e-grid-columns></ejs-grid>
[pageModel]
public class IndexModel : PageModel{public void OnGet(){}public JsonResult OnPostDataSource([FromBody]Data dm){var data = OrdersDetails.GetAllRecords();int count = data.Cast<OrdersDetails>().Count();return dm.requiresCounts ? new JsonResult(new { result = data.Skip(dm.skip).Take(dm.take), count = count }) : new JsonResult(data);}public JsonResult OnPostInsert([FromBody]CRUDModel<OrdersDetails> value){OrdersDetails.GetAllRecords().Insert(0, value.value);return new JsonResult(value.value);}public JsonResult OnPostDelete([FromBody]CRUDModel<OrdersDetails> value){OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == int.Parse(value.key.ToString())).FirstOrDefault());return new JsonResult(value);}
Help documentation : https://ej2.syncfusion.com/aspnetcore/documentation/grid/edit.html#persisting-data-in-server
Regards,R.Dhivya