Search results

State Persistence in ASP.NET Core Grid control

20 Oct 2021 / 1 minute to read

State persistence refers to the Grid’s state maintained in the browser’s localStorage even if the browser is refreshed or if you move to the next page within the browser. State persistence stores grid’s model object in the local storage when the enablePersistence is defined as true.

Maintaining custom query in a persistent state

The grid does not maintain the query params after page load event when the enablePersistence is set to true. This is because the grid refreshes its query params for every page load. You can maintain the custom query params by resetting the addParams method in actionBegin event.

Copied to clipboard
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" enablePersistence="true" actionBegin="actionBegin" allowFiltering="true" height="270"> 
        <e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Customer ID"  width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="150"></e-grid-column>


function actionBegin(args) { 
    this.query.addParams('$filter', 'EmployeeID eq 1');

Copied to clipboard
public IActionResult Index()
    var orders = OrderDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();

Get or set localStorage value

If the enablePersistence property is set to true, the grid property value is saved in the window.localStorage for reference. You can get/set the localStorage value by using the getItem/setItem method in the window.localStorage.

Copied to clipboard
//get the Grid model.
var value = window.localStorage.getItem('gridGrid'); //"gridGrid" is component name + component id.
var model= JSON.parse(model);
Copied to clipboard
//set the Grid model.
window.localStorage.setItem('gridGrid', JSON.stringify(model)); //"gridGrid" is component name + component id.