如何在MVC 4 C#网站中创建动态菜单和子菜单 [英] HOW can Creating Dynamic Menus and Submenus in an MVC 4 C# Website

查看:71
本文介绍了如何在MVC 4 C#网站中创建动态菜单和子菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVC 4 C#网站中创建动态菜单和子菜单正确的示例和编码



我想要动态创建的菜单结构

 <   ul  >  

< li > @ Html.ActionLink(主页,索引,主页)< / 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(主页,索引,主页)< / li >
< <跨度 class =code-leadattribute> li > @ Html.ActionLink(Home,Index,Home)< / li >
< li > @ Html.ActionLink( 主页,索引,主页)< / li >
< li > @ Html.ActionLink(Home,Index,Home)< / li >

< / ul >

解决方案
我已经在下面解释了动态创建示例菜单结构。

创建一个类并添加一个属性来存储li结构。如果你需要UI集合也声明这个。

  public   class 菜单
{
< span class =code-keyword> public List< string> li { get ; set ;}

}





创建一个ActionResult



  public  ActionResult Index()
{
var menu = new 菜单();
menu.li = Loadli();
return view();
}



  public 列表< string> LoadUl()
{
var list = List< string>();
list.Add( Home);
list.Add( ASP.NET);
}





创建一个返回Menu的视图。如果您正在使用任何其他类,则在该类上声明li属性,根据你的结构循环浏览项目。



 @ model菜单
{

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





希望这会有所帮助


创建一个具有以下字段的模型名称菜单

  public   int  MenuId { get ;  set ;} 
public int ParentMenuId { get ; set ;}
public str ing MenuName { get ; set ;}
public string 查看{获取; set ;}
public string 控制器{ get ; set ;}
public int 订单{获取; 设置;}





 @ model菜单
{

< ul>
@foreach( var item in Model)
{
< ; li> @ Html.ActionLink( @ Model.View @ Model.Controller @ Model.MenuName< / li>
}
< / ul>
}


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

<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>

解决方案

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.

public class Menu
{
public List<string> li{get;set;}

}



Create an ActionResult

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


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.

@model Menu
{

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



Hope this helps


Create One Model Name Menu having following fields

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;}



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


这篇关于如何在MVC 4 C#网站中创建动态菜单和子菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆