# Persisting Function References of Custom Command Buttons in the Grid

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2022.3.1109 version</td>
 </tr>
</table>

## 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`](https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event) event of both buttons.
3. Store the options of the Grid by using the [`getOptions()`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/getoptions) method while serializing them within the `LocalStorage` of the application by using the [`kendo.stringify()`](https://docs.telerik.com/kendo-ui/api/kendo/methods/stringify) method.
4. Just before you pass the options to the [`setOptions`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/setoptions) method of the Grid upon the state's persistence, add the function reference to the parsed JSON file retrieved from the `LocalStorage`.

```Razor Index.cshtml
    <a href="#" class="k-button"  id="save">Save State</a>
    <a href="#" class="k-button" 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"))
        )
    )
```

```JS script.js
    <script>
        function onClick(){
            alert("Details command is clicked !");
        }
        $(document).ready( function () {
            var grid = $("#grid").data("kendoGrid");
            $("#save").click(function (e) { // Handle the Save button's click event.
                e.preventDefault();
                localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions()); // Store the options within the LocalStorage.
            });

            $("#load").click(function (e) { // Handle the Load button's click event.
                e.preventDefault();
                var parsedOptions = JSON.parse(localStorage["kendo-grid-options"]);
                if (parsedOptions) {
                    parsedOptions.columns[1].command[0].click = onClick; // Add the function reference of the custom command.
                    grid.setOptions(parsedOptions);
                }
            });
        });
    </script>
```

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](https://netcorerepl.telerik.com/cwlwmMOt161GtEqa56).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Persist Function References to a Custom Command in the Grid](https://netcorerepl.telerik.com/cwlwmMOt161GtEqa56)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
