1

I have a database menu structure which I would like to add to the site.master file.

I’ve looked at other questions on StackOverflow but cannot get this to work on my website.

How do I add a User Control to the Site.Master file?

Menu.ascx

<%foreach (MainMenuSort mainMenuSort in (List<MainMenuSort>)ViewData["MainMenuSortListDisplay"])
      { %>
         <li><%= Html.Encode(mainMenuSort.MainMenuId.MainMenuName)%></li>
         <%foreach (SubMenuSort subMenuSort in (List<SubMenuSort>)ViewData["SubMenuSortListDisplay"])
           {%>
            <%if (mainMenuSort.MainMenuId.Id == subMenuSort.SubMenuId.MainMenu.Id)
              { %>
              <li><%= Html.Encode(subMenuSort.SubMenuId.SubMenuName)%></li>
            <%} %>
         <%} %>
      <%}%>

4 Answers 4

3

You need to use the Html.RenderPartial method in your master page.

You will need to set the MainMenuSortListDisplay and SubMenuSortListDisplay view data keys in whatever action is calling the view that uses your master page.

In your master use this

<% Html.RenderPartial("~/Views/Shared/Menu.ascx");

The path needs to be the app relative path to the control's folder. Typically these go under Shared. You can make the structure how you want below the Shared folder.

To make this technique stronger, use a strongly typed partial. In the question you would perhaps make a new class (MenuModel) with two generic collections as properties and place it in the models folder of the application. Then in the model's constructor call a method that populates the lists.

public class MenuModel
{
   public IEnumerable<MainMenuSort> OuterList {get; set;}
   public IEnumerable<SubMEnuSort> InnerList {get; set;}
   public MenuModel()
   {
       VoidThatFillsTheInnerAndOuterList();
   }

This will mean that you can do this in your controller

public ActionResult ShowAForm()
{
   ViewData["MenuPartialData"] = new MenuModel();
   return View();
}

Having set this key, your master page can use the overload of RenderPartial, like this

<% Html.RenderPartial(
    "~/View/Shared/Menu.ascx", 
    (MenuModel)ViewData["MenuPartialData"]); %>

This assumes that your partial is strongly typed to the MenuModel class. Then in the partial you can use the model which rewrites your code slightly

<% foreach (MainMenuSort mainMenuSort in Model.OuterList) { %>
     <li><%= Html.Encode(mainMenuSort.MainMenuId.MainMenuName)%></li>
     <% foreach (SubMenuSort subMenuSort in Model.InnerList) {%>            
        <%if (mainMenuSort.MainMenuId.Id == subMenuSort.SubMenuId.MainMenu.Id)  
        { %>              
           <li><%= Html.Encode(subMenuSort.SubMenuId.SubMenuName)%></li>
      <%} %>
     <%} %>      
    <%}%>

Hope that helps

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you. Just learning ASP.NET MVC and have added <% Html.RenderPartial("~/Views/Shared/Menu.ascx");%> to the site.master page but do not understand what you mean by: "You will need to set the MainMenuSortListDisplay and SubMenuSortListDisplay view data keys in whatever action is calling the view that uses your master page."
You call the page using an ActionResult method in your controller class. In this method you will need to set the ViewData keys e.g. ViewData["SubMenuSortListDisplay"] = SomeListPreferablyFromYourModel
Shout back if you're stil stuck :-)
Sorry, still cannot get the menu to display. Could you post any links to example code? Thanks.
Thank you for posting example code. The menu is now displaying in the site.master page. I used a Partial Request within the Site.master page and followed blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc . Thanks.
|
0

Try something like

<% Html.RenderPartial("Menu") %>

EDIT: Corrected a typo

Comments

0

You could also do it as a HTMLHelper and in the MasterPage just call <%= Html.Menu() %>. Then in your HTMLHelper you have the code to get the database records and loop through them. Here is a link I found to get you started. Note my comments as there is a bug in the code example provided. I'm still having issues handling subitems of menus, I guess I need a recursive function or something??

1 Comment

Thanks for posting the link. I used a Partial Request and followed blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc
0

With the help of this link. I was able to display a menu in the site.master page.

Comments

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.