Getting Started with ASP.NET Core ContextMenu Control

23 Jul 20265 minutes to read

This section briefly explains how to include the ASP.NET Core ContextMenu control in your ASP.NET Core Web App using Visual Studio.

Create ASP.NET Core Web App with Razor Pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the ASP.NET Core Extension.

Install the required ASP.NET Core packages

To add ASP.NET Core ContextMenu control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), then search and install Syncfusion.AspNetCore.Navigations and Syncfusion.AspNetCore.Themes. All Syncfusion ASP.NET Core packages are available in nuget.org. See the NuGet packages topic for more details.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.AspNetCore.Navigations -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core tag helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Navigations and Syncfusion.AspNetCore.Base tag helpers.

@addTagHelper *, Syncfusion.AspNetCore.Navigations
@addTagHelper *, Syncfusion.AspNetCore.Base

Add stylesheet and script resources

The theme stylesheet and script can be referenced from NuGet through Static Web Assets. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    ...
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    <script src="_content/Syncfusion.AspNetCore.Navigations/scripts/sf-context-menu.min.js"></script>
</head>

Register the script manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager (<ejs-scripts>) at the end of the <body> element as shown below.

<body>
    ...
    <!-- Syncfusion ASP.NET Core Script Manager -->
    <ejs-scripts></ejs-scripts>
</body>

Add the ASP.NET Core ContextMenu control

Add the ASP.NET Core ContextMenu control in the ~/Pages/Index.cshtml file.

<div id="contextmenutarget">Right click/Touch hold to open the ContextMenu </div>
<ejs-contextmenu id="contextmenu" target="#contextmenutarget" items="menuItems"></ejs-contextmenu>

<style>

#contextmenutarget {
    border: 1px dashed;
    height: 250px;
    padding: 10px;
    position: relative;
    text-align: center;
    color: gray;
    line-height: 17;
    font-size: 14px;
}

</style>

Bind dataSource

You can populate the ContextMenu with menu items by assigning a collection of items to the items property.

@{
    //define the array of JSON
    List<object> menuItems = new List<object>();
    menuItems.Add(new
    {
        text = "Cut"
    });
    menuItems.Add(new
    {
        text = "Copy"
    });
    menuItems.Add(new
    {
        text = "Paste"
    });
}

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core ContextMenu control will render in your default web browser.

ASP.NET Core ContextMenu Control

Rendering items with Separator

Separators are the horizontal lines used to separate the menu items. These separator lines are non-interactive and cannot be selected by users. To add a separator, set the separator property to true for the desired menu item.

In the following example, the Cut, Copy, and Paste options are grouped separately from the Font and Paragraph options using a separator line.

@{
    List<object> menuItems = new List<object>();
    menuItems.Add(new
    {
        text = "Cut"
    });
    menuItems.Add(new
    {
        text = "Copy"
    });
    menuItems.Add(new
    {
        text = "Paste"
    });
    menuItems.Add(new
    {
        separator = true
    });
    menuItems.Add(new
    {
        text = "Font"
    });
    menuItems.Add(new
    {
        text = "Paragraph"
    });
}

<div id="contextmenutarget">Right click/Touch hold to open the ContextMenu </div>
<ejs-contextmenu id="contextmenu" target="#contextmenutarget" items="menuItems"></ejs-contextmenu>

<style>

#contextmenutarget {
    border: 1px dashed;
    height: 250px;
    padding: 10px;
    position: relative;
    text-align: center;
    color: gray;
    line-height: 17;
    font-size: 14px;
}

</style>

NOTE

The separator property should not be given along with the other fields in the MenuItem.

ASP.NET Core ContextMenu with Separator

NOTE

View Sample in GitHub

See also

  1. Getting Started with ASP.NET Core in Visual Studio Mac
  2. Getting Started with ASP.NET Core MVC using Tag Helper