# Adding a Common Function Delegate Handler on Events

## Environment

<table>
	<tbody>
		<tr>
			<td>Product</td>
			<td>Telerik UI for ASP.NET MVC</td>
		</tr>
	</tbody>
</table>

## Description

How can I create common function handler that can be reused in different modules. But with supplemented parameters of different kind based on custom logic? For example, in the Telerik UI for ASP.NET MVC Grid columns.

## Solution

In the context of Telerik UI for ASP.NET MVC Helpers, adding a function with additional parameters requires a given event to expose a delegate overload.

Most of the Telerik UI for ASP.NET MVC Components event handlers provide this overload. This allows you to subscribe to events by a  [`Func<TResult>`](https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=netframework-4.7.2) delegate.

The following example demonstrates how to achieve the desired outcome:

1. Utilize the [.Click()](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridcustomactioncommandbuilder#clicksystemfunc) configuration method in order to employ a delegate.
2. Within the delegate, pass an [anonymous function](https://www.geeksforgeeks.org/javascript-anonymous-functions/).
3. Inside the anonymous function, call a common function handler with additionally passed arguments.

```Razor HtmlHelper
   @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>()
        .Name("Grid")
        .Columns(columns => {
            columns.Bound(e => e.FirstName);
            columns.Bound(e => e.LastName);
            columns.Bound(e => e.Title);
            columns.Command(command =>
            {
                command.Custom("ViewEnvelope")
                        .Click(@<text>
                            function(){
                                envelopeAction(event, "View")
                            }
                        </text>)
                        .Text("View")
                        .IconClass("k-icon k-i-envelop")
                        .HtmlAttributes(new { title = "View Envelope" });
                
                command.Custom("ApproveEnvelope")
                        .Click(@<text>
                            function(){
                                envelopeAction(event, "Approve")
                            }
                        </text>)
                        .Text("Approve")
                        .IconClass("k-icon k-i-check-circle")
                        .HtmlAttributes(new { title = "Approve Envelope" });
                
                command.Custom("RejectEnvelope")
                        .Click(@<text>
                            function(){
                                envelopeAction(event, "Reject")
                            }
                        </text>)
                        .Text("Reject")
                        .IconClass("k-icon k-i-cancel")
                        .HtmlAttributes(new { title = "Reject Envelope" });
            }).Width(350);
        })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("CustomCommand_Read", "Grid"))
        )
    )
```

```JS script.js
    <script>
        function envelopeAction(e, action){
            switch(action){
                case "View":
                    alert("View Action Clicked")
                break;
                case "Approve":
                    alert("Approve Action Clicked")
                break;
                case "Reject":
                    alert("Reject Action Clicked")
                break;
            }
        }    
    </script>
```

For a runnable example based on the code above, refer to the [REPL example on adding a common function delegate handler with additional parameters on events](https://netcorerepl.telerik.com/ceuLPqFe26FY1u6A24).

## 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

* [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 REPL for Adding a Common Function handler through a Delegate](https://netcorerepl.telerik.com/ceuLPqFe26FY1u6A24)

* [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)
