New to Telerik UI for ASP.NET MVCStart a free 30-day trial

Persisting Function References of Custom Command Buttons in the Grid

Environment

ProductTelerik UI for ASP.NET MVC Grid
Progress Telerik UI for ASP.NET MVC versionCreated with the 2022.3.1109 version

Description

How can I persist the function references of the custom command buttons in the Telerik UI for ASP.NET MVC Grid after serialization?

Solution

To achieve the desired scenario:

  1. Create two separate buttons which will be responsible for the loading and persisting of the Grid's state.
  2. Handle the click event of both buttons.
  3. Store the options of the Grid by using the getOptions() method while serializing them within the LocalStorage of the application by using the kendo.stringify() method.
  4. Just before you pass the options to the setOptions method of the Grid upon the state's persistence, add the function reference to the parsed JSON file retrieved from the LocalStorage.
Index.cshtml
    <a href="#" class="k-button k-button-md k-rounded-md k-button-solid-base"  id="save">Save State</a>
    <a href="#" class="k-button k-button-md k-rounded-md k-button-solid-base" id="load">Load State</a>

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CustomerViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.ContactName).Width(400);
            columns.Command(c => c.Custom("View Details").Click("onClick")).Width(200);
        })
        .HtmlAttributes(new { style = "height: 550px;" })
        .Scrollable()
        .Groupable()
        .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
        .Sortable()
        .ColumnMenu()
        .Resizable(rsb=>rsb.Columns(true))
        .Reorderable(r => r.Columns(true))
        .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("Customers_Read", "Grid"))
        )
    )

For the complete implementation of the suggested approach, refer to the Telerik REPL example on persisting function references to a custom command in the Grid.

More ASP.NET MVC Grid Resources

See Also