在ASP.Net MVC3中使用HtmlHelper和Partial Page渲染视图

时间:2014-01-06 11:30:49

标签: c# css asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

你好我的项目我必须使用ASP.NET MVC3 Razor实现动态菜单..我的菜单结构是

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

对于上面的结构,我有一个布局页面(带有css和脚本),它的工作正常。 问题是在我的视图页面菜单中没有以正确的格式显示 。有两个HtmlHelperExtensions,一个用于主菜单,另一个用于Sub菜单。它们在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>

    }
  

如何在调用父菜单时动态调用子菜单。这是   明确地将主菜单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>

1 个答案:

答案 0 :(得分:2)

你可以循环菜单项并像这样传递MainMenuId

@foreach(var menuItem in Model)
{

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

}