Search results

Open and close ContextMenu in ASP.NET MVC ContextMenu control

28 Oct 2021 / 1 minute to read

Open and close the ContextMenu manually whenever required by using the open and close methods. In the following sample, to open the ContextMenu at specified position the open method is used with X and Y coordinates and to close ContextMenu close method can be used.

razor
openclose.cs
Copied to clipboard
@Html.EJS().Button("button").Content("Open ContextMenu").Render()
@Html.EJS().ContextMenu("contextmenu").Items((IEnumerable<object>)ViewBag.menuItems).Render()

<script>
        document.addEventListener("DOMContentLoaded", function () {
            document.getElementById('button').onclick = function () {
                ej.base.getComponent(document.getElementById("contextmenu"), "contextmenu").open(60, 20);
            }
        });

    </script>

    <style>

        button {
            margin: 20px 0 0 5px;
        }
    </style>

</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 ActionResult OpenClose()
        {
            List<object> menuItems = new List<object>();
            menuItems.Add(new
            {
                text = "Cut"
            });
            menuItems.Add(new
            {
                text = "Copy"
            });
            menuItems.Add(new
            {
                text = "Paste"
            });
            ViewBag.menuItems = menuItems;
            return View();
        }
    }
}