We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Ability to call an action when when MenItem clicked

I am trying to implement a menu bar, however I need the ability for some of the menu items to open a modal window.  I notice that the MenuItem class has a Text and Url property but it doesn't appear to have any event handlers.  Is it possible to specify an action when certain menu items are clicked vs. just navigating to another page?

9 Replies

SP Sangeetha Priya Murugan Syncfusion Team August 14, 2019 12:26 AM

Hi Dominick, 

Thank you for contacting Syncfusion support. 

We have checked your reported requirement and it can be achievable in Menu by using ItemSelected event as like in the below code example. 

CODE SNIPPETS: 

<EjsMenu Items="@menuItems"> 
    <MenuEvents ItemSelected="onSelect"></MenuEvents> 
</EjsMenu> 
<div id="target"> 
    <EjsDialog @ref="DialogObj" Target="#target" Height="270px" Width="500px" ShowCloseIcon="true" @bind-Visible="@Visibility"> 
        ..// 
    </EjsDialog> 
    </div> 
 
@code{ 
    EjsDialog DialogObj; 
    public bool Visibility { get;set; } = false; 
    private List<MenuItem> menuItems = new List<MenuItem>{ 
            new MenuItem 
            { 
                Text = "File", 
                Items = new List<MenuItem> 
                { 
                    new MenuItem { Text= "Open" },  
                    new MenuItem { Text= "Navigate", Url="counter" },  // Url property, for navigating to counter page. 
                } 
            }, 
             ..// 
    }; 
 
    private void onSelect(MenuEventArgs args) 
    { 
        if (args.Item.Text == "Open"// To check the selected item text 
            DialogObj.Show();  // To open the modal dialog. 
    } 
    } 

For your convenience, we have prepared the sample based on our requirement. Please find the link below. 


Could you please check the above sample and get back to us, if you need any further assistance on this. 

Regards, 
Sangeetha M 



J j January 12, 2020 01:15 AM

This is nice, I used this.


SD Saranya Dhayalan Syncfusion Team January 12, 2020 09:05 PM

Hi Dominick, 
 
Thanks for the update 
 
Please let us know, if you need any further assistance on this.  
 
Regards, 
Saranya D 



PH Philippe March 31, 2021 01:03 PM

Your sample don't work

I try all your method, nothing working


AS Aravinthan Seetharaman Syncfusion Team April 1, 2021 02:13 AM

Hi Philippe, 
 
We have checked your query. We cannot reproduce your reported issue in our latest version v19.1.0.54. For your reference we have prepared code snippet and Sample here. 
 
Index.razor 
@using Syncfusion.Blazor.Navigations 
 
<div class="control-section"> 
    <div class="menu-control"> 
        <SfMenu TValue="MenuItem"> 
            <MenuItems> 
                <MenuItem Text="File"> 
                    <MenuItems> 
                        <MenuItem Text="Open"></MenuItem> 
                        <MenuItem Text="Save"></MenuItem> 
                        <MenuItem Separator="true"></MenuItem> 
                        <MenuItem Text="Exit"></MenuItem> 
                    </MenuItems> 
                </MenuItem> 
 
                <MenuItem Text="Edit"> 
                    <MenuItems> 
                        <MenuItem Text="Cut" ></MenuItem> 
                        <MenuItem Text="Copy" ></MenuItem> 
                        <MenuItem Text="Paste"></MenuItem> 
                    </MenuItems> 
                </MenuItem> 
 
                <MenuItem Text="View"> 
                    <MenuItems> 
                        <MenuItem Text="Toolbars"> 
                            <MenuItems> 
                                <MenuItem Text="Menu Bar"></MenuItem> 
                                <MenuItem Text="Bookmarks Toolbar"></MenuItem> 
                                <MenuItem Text="Customize"></MenuItem> 
                            </MenuItems> 
                        </MenuItem> 
                        <MenuItem Text="Zoom"> 
                            <MenuItems> 
                                <MenuItem Text="Zoom In"></MenuItem> 
                                <MenuItem Text="Zoom Out"></MenuItem> 
                                <MenuItem Text="Reset"></MenuItem> 
                            </MenuItems> 
                        </MenuItem> 
                        <MenuItem Text="Full Screen"></MenuItem> 
                    </MenuItems> 
                </MenuItem> 
                <MenuItem Text="Tools"> 
                    <MenuItems> 
                        <MenuItem Text="Spelling & Grammar"></MenuItem> 
                        <MenuItem Text="Customize"></MenuItem> 
                        <MenuItem Separator="true"></MenuItem> 
                        <MenuItem Text="Options"></MenuItem> 
                    </MenuItems> 
                </MenuItem> 
                <MenuItem Text="Help"></MenuItem> 
            </MenuItems> 
            <MenuEvents ItemSelected="Select" TValue="MenuItem"></MenuEvents> 
        </SfMenu> 
 
    </div> 
</div> 
 
@code { 
    private void Select(MenuEventArgs<MenuItem> args) 
    { 
        // Your code here. 
    } 
} 
 
 
 
If you are still facing the issue, kindly share the below details. 
 
·        If possible, try to reproduce the reported issue in provided sample or share the issue reproducible sample. 
·        Please share us the video demonstration of this issue. 
·        Please share us the Syncfusion Package Version. 
 
Please provide the above requested information, based on that we will check and provide you a better solution quickly. 
 
Regards, 
Aravinthan S 



PH Philippe April 3, 2021 12:55 PM

I just try it same message

      Unhandled exception in circuit 'poIko7koj_Qto1SGaWQ6fd1QPACE3mYdvTIpiHweU9U'.
      System.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[Syncfusion.Blazor.Navigations.MenuEventArgs`1[ProMobileWeb.MenuGauche+DataModel]]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[Syncfusion.Blazor.Navigations.MenuEventArgs`1[Syncfusion.Blazor.Navigations.MenuItemModel]]'.
         at Syncfusion.Blazor.Internal.SfBaseUtils.InvokeEvent[T](Object eventFn, T eventArgs)
         at Syncfusion.Blazor.Navigations.Internal.CreateMenuItem`2.ItemClickHandler(TItem item, EventArgs e, Boolean isEnterKey)
         at Syncfusion.Blazor.Navigations.Internal.CreateMenuItem`2.<>c__DisplayClass36_0.<<BuildRenderTree>b__1>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit 'poIko7koj_Qto1SGaWQ6fd1QPACE3mYdvTIpiHweU9U'.


Some parts of my code

                            <SfMenu CssClass="dock-menu"
                                    Items="@menuItems"
                                    Orientation="Orientation.Vertical"
                                    ShowItemOnClick="true"
                                    EnablePersistence="true"
                                    TValue="DataModel">
                                <MenuEvents ItemSelected="Select"
                                            TValue="DataModel">
                                </MenuEvents>
                            </SfMenu>

@code {
    SfSidebar sidebar;
    public bool SidebarToggle = false;

    private void Select(MenuEventArgs<DataModel> args)
    {
        Close();
    }

    public class DataModel
    {
        public string Id { get; set; }
        public string Text { get; set; }
        public string IconCss { get; set; }
        public string Url { get; set; }
        public string ParentId { get; set; }
    }
    private List<DataModel> menuItems = new List<DataModel>();


    protected override void OnInitialized()
    {
        base.OnInitialized();
        menuItems.Add(new DataModel
        {
            Id = "01",
            Text = @languageContainer.Keys["Articles"],
            IconCss = "fas fa-file-word"
        });
        menuItems.Add(new DataModel
        {
            Id = "0101",
            Text = @languageContainer.Keys["VoirEdition"],
            IconCss = "fas fa-file-word",
            Url = "admarticles",
            ParentId = "01"
        });
        menuItems.Add(new DataModel
        {
            Id = "0102",
            Text = @languageContainer.Keys["AjouterUnArticle"],
            IconCss = "fas fa-plus-square",
            Url = "admarticlesdetail/0",
            ParentId = "01"
        });
        menuItems.Add(new DataModel
        {
            Id = "02",
            Text = @languageContainer.Keys["Endroits"],
            IconCss = "fas fa-map-marker-alt"
        });

EVERYTHING IS OK BUT I CAN CAPTURE THE ONSELECT TO CLOSE THE SIDEBAR


AS Aravinthan Seetharaman Syncfusion Team April 5, 2021 03:26 AM

 
Thanks for the update. 
 
We have checked your query. We suspect that this issue occurred due to giving custom type while using self-referential data structure. In self-referential item defining method we have used strongly typed value MenuItemModel to populate submenu items. So, we can resolve this issue by providing strongly typed value MenuItemModel as TValue in MenuEvents and in arguments. Please refer the below code snippet. 
 
Index.razor 
 
@using Syncfusion.Blazor.Navigations 
 
<SfMenu CssClass="dock-menu" 
        Items="@menuItems" 
        Orientation="Orientation.Vertical" 
        ShowItemOnClick="true" 
        EnablePersistence="true" 
        TValue="DataModel"> 
    <MenuEvents ItemSelected="Select" 
                TValue="MenuItemModel"> 
    </MenuEvents> 
</SfMenu> 
@code { 
    public bool SidebarToggle = false; 
 
    private void Select(MenuEventArgs<MenuItemModel> args) 
    { 
        //Your code here. 
    } 
 
    public class DataModel 
    { 
        public string Id { get; set; } 
        public string Text { get; set; } 
        public string IconCss { get; set; } 
        public string Url { get; set; } 
        public string ParentId { get; set; } 
    } 
    private List<DataModel> menuItems = new List<DataModel>(); 
 
 
    protected override void OnInitialized() 
    { 
        base.OnInitialized(); 
        menuItems.Add(new DataModel 
        { 
            Id = "01", 
            Text = "One", 
            IconCss = "fas fa-file-word" 
        }); 
        menuItems.Add(new DataModel 
        { 
            Id = "0101", 
            Text = "Two", 
            IconCss = "fas fa-file-word", 
            Url = "admarticles", 
            ParentId = "01" 
        }); 
        menuItems.Add(new DataModel 
        { 
            Id = "0102", 
            Text = "Three", 
            IconCss = "fas fa-plus-square", 
            Url = "admarticlesdetail/0", 
            ParentId = "01" 
        }); 
        menuItems.Add(new DataModel 
        { 
            Id = "02", 
            Text = "Four", 
            IconCss = "fas fa-map-marker-alt" 
        }); 
    } 
} 
 
 
 
Could you please check the above details, and get back to us, if you need assistance on this. 
 
Regards, 
Aravinthan S


PH Philippe April 5, 2021 01:04 PM

Thank you everything working now


SP Sangeetha Priya Murugan Syncfusion Team April 5, 2021 11:07 PM

Hi Philippe, 
 
Thank you for your update. 
 
We are happy to hear that your issue has been resolved. Kindly get back to us if you need any further assistance. 
 
Regards, 
Sangeetha M 


Loader.
Live Chat Icon For mobile
Up arrow icon