New to Telerik UI for ASP.NET Core? Download free 30-day trial

ContextMenu in Razor Pages

Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by design, the two files have the same name).

You can seamlessly integrate the Telerik UI ContextMenu for ASP.NET Core in Razor Pages applications.

This article describes how to configure the ContextMenu component in a Razor Pages scenario.

For the complete project, refer to the ContextMenu in Razor Pages example.


    @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
    @Html.AntiForgeryToken()

    <div id="target">Right click here</div>

    @(Html.Kendo().ContextMenu()
        .Name("RequestMenu")
        .Target("#target")
        .Orientation(ContextMenuOrientation.Vertical)
        .Items(items =>
        {
            items.Add()
                .Text("Edit");

            items.Add()
                 .Text("Cancel");
        })
        .Events(e =>
        {
            e.Select("onSelect");

        })
    )

    <script>
        function onSelect(e) {
            if ($(e.item).text() == "Edit") {
                $.ajax({
                    url: "/ContextMenu/ContextMenuIndex?handler=Custom",
                    type: "POST",
                    contentType: "application/json",
                    headers: {
                        RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
                    }
                });
            }        
        }
    </script>
    <div id="target">Right click here</div>

    <kendo-contextmenu name="menu" target="#target" orientation="ContextMenuOrientation.Vertical"
        on-select="onSelect">
        <items>
            <menu-item text="Edit">
            </menu-item>
            <menu-item text="Cancel">
            </menu-item>
        </items>
    </kendo-contextmenu>

    <script>
        function onSelect(e) {
            if ($(e.item).text() == "Edit") {
                $.ajax({
                    url: "/ContextMenu/ContextMenuIndex?handler=Custom",
                    type: "POST",
                    contentType: "application/json",
                    headers: {
                        RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
                    }
                });
            }        
        }
    </script>

    public void OnPostCustom()
        {
            ....
        }

See Also

In this article