呈现在ASP.Net MVC3

问题描述:

你好用的HtmlHelper和部分页面视图中的项目中,我实现使用ASP.NET MVC3剃刀..My菜单结构动态菜单是呈现在ASP.Net MVC3

1.UserManagement 1.1Create新用户 1.2Create新角色 2.Accounts 2.1Income 2.2Expense

对于上述结构我有一个页面布局(CSS和脚本)和它的工作的罚款。 的问题是,在我看来,页面菜单不正确的格式 显示。有两种HtmlHelperExtensions一个是主菜单和其他为副menu.Both在Menus.cshtml的部分页面初始化

@model IEnumerable<Elixir.Models.Menus> 

<div> 
    @Html.Raw(@Html.ParentMenus(Model)) 



    </div> 

    <div> 

    @Html.Raw(@Html.SubMenu(Model,3)) 
    </div> 

更新部分代码代码

@model IEnumerable<Elixir.Models.Menus> 


    @Html.Raw(@Html.ParentMenus(Model)) 

    @foreach (var menuitem in Model) 
    { 


     <div> 
     @Html.Raw(@Html.ParentMenus(Model)) 
     </div> 
     <div> 
     @if (menuitem.MainMenuId == menuitem.MenuId) 
     { 
      int ab = Convert.ToInt32(menuitem.MainMenuId); 
    @Html.Raw(@Html.SubMenu(Model, ab)) 
     } 
    </div> 

    } 

如何调用子菜单时动态父菜单called.Here是 明确将主菜单ID分配为子菜单中的“3”。

如何连接父菜单Html助手与子菜单?

的HtmlHelper扩展代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Html; 
using Elixir.Models; 

namespace Elixir.HtmlHelpers 
{ 
    public static class HtmlHelperExtensions 
    { 
     public static string ParentMenus(this HtmlHelper html,IEnumerable<Elixir.Models.Menus> menu) 
     { 
      string htmloutput=string.Empty; 
      if (menu.Count() > 0) 
      { 
       htmloutput += "<ul class='side-navigation accordion' id='nav-accordion'>"; 
       var MainMenu = from mainMenu in menu where mainMenu.MainMenuId == null orderby mainMenu.MenuOrder select mainMenu; 
       foreach (Menus m in MainMenu) 
       { 
        htmloutput += "<li>"; 
        htmloutput += "<li><i class='icon-home'>"; 
        htmloutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName); 
        htmloutput += "</li>"; 
        htmloutput += "</li></i>"; 
       } 
       htmloutput += "</ul>"; 
      } 

      return htmloutput; 

     } 

     public static string SubMenu(this HtmlHelper html, IEnumerable<Menus> SubMenu, int MenuId) 
     { 
      string htmlOutput = string.Empty; 
      if (SubMenu.Count() > 0) 
      { 
       htmlOutput += "<ul class='side-navigation accordion' id='nav-accordion'>"; 
       var subMenu = from SMenu in SubMenu where SMenu.MainMenuId == MenuId orderby SMenu.MenuOrder select SMenu; 
       foreach (Menus m in subMenu) 
       { 
        htmlOutput += "<li>"; 
        htmlOutput += "<li><i class='icon-home'>"; 
        htmlOutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName); 
        htmlOutput += "</li>"; 
        htmlOutput += "</li></i>"; 
       } 
       htmlOutput += "</ul>"; 
      } 
      return htmlOutput; 
     } 

    } 
} 

我的索引查看代码

@{ 
    ViewBag.Title = "Elixir ERP V1.0 Beta"; 
    Layout = "~/Views/Shared/_LayoutUser.cshtml"; 
} 
<div class="main-container"> 
    <div class="main-wrapper"> 
     <div class="scroll-top"> 
      <a href="#" class="tip-top" title="Go Top"><i class="icon-arrow-up"></i></a> 
     </div> 
     <div class="left-bar merge-left"> 
      <!-- SEARCH BAR --> 
      <!-- LEFT NAV --> 
      @section leftnav{ 

      <div class="left-nav"> 
      @Html.Action("Menus"); 
    </div> 



      } 
     </div> 
    </div> 
    <div class="container"> 
    </div> 
</div> 

你可以循环的菜单项,并通过在MainMenuId这样的:

@foreach(var menuItem in Model) 
{ 

    <div> 
    @Html.Raw(@Html.ParentMenus(menuItem)) 
    </div> 
    <div> 
    @Html.Raw(@Html.SubMenu(menuItem, menuItem.MainMenuId)) 
    </div> 

} 
+0

您好,我正在为上述解决方案执行处理程序'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错。 – Nidheesh

+0

当您将原始问题的值3硬编码时,它是否工作?这可能是查找菜单操作的数据或问题。 – hutchonoid

+0

是的,现在我已经用你的逻辑更新了问题,现在没有错误,但菜单同时显示所有字段值。 – Nidheesh