Search results

Open a dialog on ContextMenu item click in ASP.NET MVC ContextMenu control

03 Mar 2021 / 1 minute to read

This section explains about how to open a dialog on ContextMenu item click. This can be achieved by handling dialog open in select event of the ContextMenu.

In the following sample, Dialog will open while clicking Save As... item:

razor
dialog.cs
Copied to clipboard
<div id="contextmenutarget">Right click/Touch hold to open the ContextMenu </div>
@Html.EJS().ContextMenu("contextmenu").Target("#contextmenutarget").Items((IEnumerable<object>)ViewBag.menuItems).Select("itemSelect").Render()
@Html.EJS().Dialog("dialog").Header("Save As").Content("This file can be saved as PDF").Width("250px").Visible(false).Buttons(ViewBag.DialogButtons).Render()

<script>
    function dlgButtonClick() {
        ej.base.getComponent(document.getElementById('dialog'), "dialog").hide();
    }

    function itemSelect() {
        ej.base.getComponent(document.getElementById('dialog'), "dialog").show();
    }
</script>

<style>

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

</style>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    public class ContextMenuController : Controller
    {
        public IActionResult Dialog()
        {
            List<object> menuItems = new List<object>();
            menuItems.Add(new
            {
                text = "Back"
            });
            menuItems.Add(new
            {
                text = "Forward"
            });
            menuItems.Add(new
            {
                text = "Reload"
            });
            menuItems.Add(new
            {
                separator= true
            });
            menuItems.Add(new
            {
                text = "Save As..."
            });
            menuItems.Add(new
            {
                text = "Print"
            });
            menuItems.Add(new
            {
                text = "Cast"
            });
            ViewBag.menuItems = menuItems;
            return View();
        }
    }
}