Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HOW can Creating Dynamic Menus and Submenus in an MVC 4 C# Website proper example and coding

Menu Structure that i want to create dynamically
HTML
<ul>
       
    <li>  @Html.ActionLink("Home","Index","Home")</li>
    <li> @Html.ActionLink("Home","Index","Home")</li>
    <li>@Html.ActionLink("ASP.NET","ASPPage","ASPNET")
        <ul>
        <li>@Html.ActionLink("ASP.NET","ASPPage","ASPNET")</li>
        <li>@Html.ActionLink("ASP.NET","ASPPage","ASPNET")</li>
        <li>@Html.ActionLink("ASP.NET","ASPPage","ASPNET")</li>
        <li>@Html.ActionLink("ASP.NET","ASPPage","ASPNET")</li>
        </ul>
    </li>
    <li>  @Html.ActionLink("Home","Index","Home")</li>
    <li> @Html.ActionLink("Home","Index","Home")</li>
    <li>  @Html.ActionLink("Home","Index","Home")</li>
    <li> @Html.ActionLink("Home","Index","Home")</li>
   
    </ul>
Posted
Updated 22-Jul-13 0:34am
v2
Comments
Jameel VM 22-Jul-13 4:52am    
did you try anything?where you are stuck?
krishna97 22-Jul-13 5:06am    
ya..sir Dynamic Menu And Submenu for MVC4
krishna97 22-Jul-13 5:06am    
example and coding
Jameel VM 22-Jul-13 5:16am    
where you are stuck?Please post the code what you have tried
krishna97 22-Jul-13 5:19am    
My code not a proper way ,,,,,??
sir you can post me for MVC code and C# and databse design plz sir ....??

I have explained below for creating a sample menu structure dynamically.
Create a class and add a property for storing li structure.If u need UI collection declare this too.
C#
public class Menu
{
public List<string> li{get;set;}

}


Create an ActionResult

C#
public ActionResult Index()
{
    var menu=new Menu();
menu.li=Loadli();
return view();
}

C#
public List<string> LoadUl()
{
var list=List<string>();
list.Add("Home");
list.Add("ASP.NET");
}


Create a view which return Menu.If you are using anyother class then declare the li property on that class and loop through the item according to your structure.

C#
@model Menu
{

<ul>
  @foreach (var item in Model.li)
            {
                <li>@item</li>
            }
</ul>
}


Hope this helps
 
Share this answer
 
Create One Model Name Menu having following fields
C#
public int MenuId{get;set;}
public int ParentMenuId{get;set;}
public string MenuName{get;set;}
public string View{get;set;}
public string Controller{get;set;}
public int Order{get;set;}


C#
@model Menu
{
 
<ul>
  @foreach (var item in Model)
            {
                <li>@Html.ActionLink("@Model.View","@Model.Controller","@Model.MenuName</li>
            }
</ul>
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900