1

I want to load menu dynamically in layout page in MVC.

I have created Viewcode, model for menu and menuitems. Now what I want is to load the menus dynamically. It has to retrieve data from database and load in Layout page.

My model

Menu Model

public Menu()
{
    MenuItems = new List<MenuItem>();
}

public int Id { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }

My model MenuItem

public class MenuItem
{
public int Id { get; set; }
public int UserName { get; set; }
public string Name { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
public string Url { get; set; }
public Menu ParentMenu { get; set; }
}

My Controller Code

  public ActionResult Index()
    {
        var menu = new Menu();
        menu.Name = "Main Menu";
        var query = db.ApplicationNames.Select(e => new { 
          UID=  e.UID,
             e.ApplicationName1
        }).FirstOrDefault();
        var uid = query.UID.ToString();
        var name = query.ApplicationName1;

   var items = new List<MenuItem>() { new MenuItem { Name = "FirstMenu", ControllerName = uid, ActionName = name }};
        menu.MenuItems = items;
        return PartialView("_MenuLayout",menu);
    }

My partial view _MenuLayout

 @model List<DynamicMenuLoading.Models.Menu>
        <ul>
             <li>
                <a href="#">
                   <span>@Model.Name</span>
                  </a>
                <ul>
                    @foreach (var item in Model.MenuItems)
                    {
                        <li><a href="@Url.Action(@item.ActionName , @item.ControllerName )"><i class="fa fa-circle-o"></i>@item.Name</a></li>
                    }
                </ul>
            </li>
        </ul>

My Layout Page

         <nav id="mainNavigation">
           @Html.Partial("_MenuLayout")
         </nav>

My table name ApplicationName

UID ApplicationName

1 EMPLOYEE

2 CUSTOMER

3 CITY

4 STATE

5 COUNTRY

While I am trying to run my project I am getting this error:

e:\Task Tried\DynamicMenuLoading\DynamicMenuLoading\Views\Shared_MenuLayout.cshtml(8): error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

Can anyone tell me what mistake I did? Can anyone understand my issue and give solution to my problem?

1 Answer 1

1

Conceptual problem: You need to create a separate controller action to retrieve the menu data and display the menu view.

public ActionResult MenuLayout()
{
    var menu = new Menu();

    // TODO: Fill menu with data here

    return PartialView("_MenuLayout", menu);
}

Call that Action from the layout page:

<nav id="mainNavigation">
    @Html.Action("MenuLayout", "Home")
</nav>

Also, in your partial view, use a model of class Menu, not a list of objects of that class:

@model DynamicMenuLoading.Models.Menu
Sign up to request clarification or add additional context in comments.

2 Comments

nineberry i did all this separate controller only
can you explain your answer in detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.