Example of Remote Data (Load Once) in ASP.NET Core Data Grid Control
This sample demonstrates the way of data binding Grid component with remote service. The Grid data source is bound to remote data using DataManager.
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "WebApi",
DataUrl = Url.Action("UrlDatasource", "GridDemo"),
AllowPaging = true,
PageSize = 12,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 160 },
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150 }
}
})
@Html.AntiForgeryToken()
<style>
#export-cache-container { display: flex; margin-bottom: 7px; }
#export-cache-container label { font-size: 15px; margin-right: 8px; }
</style>
<div style="display: flex;">
<div id="export-cache-container">
<label for="checked" id="export"> Enable Cache </label>
<input type="checkbox" id="checked" role="switch" />
</div>
</div>
@Html.UiGrid(new UiGridOptions
{
Id = "WebApi",
DataUrl = Url.Action("UrlDatasource", "GridDemo"),
AllowPaging = true,
PageSize = 12,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 160 },
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150 }
}
})
<script>
// Syncfusion: Enable Cache (loadOnce) reads every order once and pages them in the browser; without it each page is a request.
document.getElementById('checked').addEventListener('change', function (e) {
window.Lumen.get('WebApi').changeDataSource(e.target.checked
? { dataUrl: '@Url.Action("RemoteOrders", "GridDemo")', serverOperations: false }
: { dataUrl: '@Url.Action("UrlDatasource", "GridDemo")', serverOperations: true });
});
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/LoadOnce.cshtml; it draws what ViewData holds.
public ActionResult LoadOnce()
{
// the grid reads its pages from the UrlDatasource action; with the cache on, every order once from RemoteOrders
return View();
}
/// <summary>One order: the rows of Syncfusion's Grid samples that use the orders.</summary>
public class OrdersDetails
{
public OrdersDetails()
{
}
public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.Verified = Verified;
this.OrderDate = OrderDate;
this.ShipName = ShipName;
this.ShipCountry = ShipCountry;
this.ShippedDate = ShippedDate;
this.ShipAddress = ShipAddress;
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipCity { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public DateTime ShippedDate { get; set; }
public string ShipAddress { get; set; }
public static List<OrdersDetails> GetAllRecords()
{
List<OrdersDetails> order = new List<OrdersDetails>();
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15, 10, 40, 00), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16, 09, 30, 00), "Kirchgasse 6"));
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04, 09, 30, 00), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11, 03, 45, 00), "Avda. Azteca 123"));
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30, 03, 45, 00), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7, 07, 50, 00), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22, 07, 30, 00), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30, 06, 12, 00), "Magazinweg 7"));
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18, 05, 50, 00), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3, 12, 50, 00), "1029 - 12th Ave. S."));
code += 5;
}
return order;
}
}
/// <summary>The Load Once sample's orders as one plain array (ServerOperations = false): the grid reads them once, then pages, sorts and filters them in the browser.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult RemoteOrders()
{
return Json(OrdersDetails.GetAllRecords());
}
/// <summary>The URL Adaptor sample's orders: UiDataOperations.Execute pages, sorts, filters and searches them on the server (Syncfusion's UrlDatasource action with DataOperations).</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult UrlDatasource(UiDataRequest request)
{
return Json(UiDataOperations.Execute(OrdersDetails.GetAllRecords().AsQueryable(), request, "OrderID"));
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The Grid supports data binding. The DataSource property can be assigned with the instance of DataManager to bind remote data.
The DataManager, which will act as an interface between the service endpoint and the Grid, will require the below minimal information to interact with service endpoint properly.
DataManager->url- Defines the service endpoint to fetch dataDataManager->adaptor- Defines the adaptor option. By default,ODataAdaptoris used for remote binding.
Adaptor is responsible for processing response and request from/to the service endpoint. syncfusion/ej2-data package provides some predefined adaptors which are designed to interact with particular service endpoints. They are,
UrlAdaptor- Use this to interact any remote services. This is the base adaptor for all remote based adaptors.ODataAdaptor- Use this to interact with OData endpoints.ODataV4Adaptor- Use this to interact with OData V4 endpoints.WebApiAdaptor- Use this to interact with Web API created under OData standards.WebMethodAdaptor- Use this to interact with web methods.
In this demo, remote data is bound by assigning service data as an instance of DataManager to the DataSource property.
The DataManager provides an option to avoid sending requests for previously visited pages by enabling the enableCache property. When this property is enabled, the DataManager does not send a request to the server when revisiting a page. However, the cache will be reset if any data action, such as sorting or filtering, is performed.