ASP.NET MVC Menu Selected Item

前端 未结 7 1497
夕颜
夕颜 2021-01-30 18:29

OK new to MVC. I had asked this question earlier and got an answer but I am wondering if there is a simpler solution.

Say I have a master page with a menu laid out as an

7条回答
  •  独厮守ぢ
    2021-01-30 18:51

    You should pass all relevant information in the Model. Ideally your menu will be rendered as a Partial View by a separate controller method. I have a Navigation controller with actions like MainMenu, FooterMenu, Breadcrumbs, etc that render individual parts.

    Your model will be a collection of menu items like:

        public class MenuItemModel
        {
            public MenuItemModel()
            {
                SubMenu = new List();
            }
    
            public string Text { get; set; }
            public string Controller { get; set; }            
            public string Action { get; set; }
            public bool Selected { get; set; }
    
            public List SubMenu { get; private set; }
        }
    

    Your Controller will create a collection of menu items and pass them to the view with the appropriate item selected. Then the view can be as simple as:

    
    

提交回复
热议问题