关于mvc4中的多个母版页

时间:2013-05-10 14:44:52

标签: javascript asp.net-mvc asp.net-mvc-4

enter image description here我有一个名为 Site.Master 的母版页,其中我有标签逻辑

  <div id="menubar">
        @Html.SimpleNav(new[] {
            new SimpleNavItem{Text="Home", Action="Index", Controller = "Home",GetSelected = ((a, c) => (c == "Home" && a == "Index"))},
            new SimpleNavItem{Text="Blog", Action="Index", Controller = "Blog", GetSelected = ((a, c) => (c == "Blog" && a == "Index"))},
            new SimpleNavItem{Text="About", Action="About", Controller = "Home", GetSelected = ((a, c) => c == "Home" && a == "About")},
            new SimpleNavItem{Text="Contact", Action="Contact", Controller = "Home", GetSelected = ((a, c) => c == "Home" && a == "Contact")}
        }) %>
    </div>

Tab逻辑类文件代码是这个

 public static class SimpleNavExtensions
{
    public static string SimpleNav(this HtmlHelper html, IEnumerable<SimpleNavItem> navItems)
    {
        var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        string controller = html.ViewContext.RouteData.Values["controller"].ToString();
        string action = html.ViewContext.RouteData.Values["action"].ToString();

        TagBuilder ul = new TagBuilder("ul");
        ul.AddCssClass("clearfix");

        StringBuilder listBuilder = new StringBuilder();
        TagBuilder li = null;
        TagBuilder a = null;
        foreach (var item in navItems)
        {
            a = new TagBuilder("a");
            a.Attributes.Add("href", urlHelper.Action(item.Action, item.Controller));
            a.InnerHtml = item.Text;

            li = new TagBuilder("li");
            if (item.GetSelected != null && item.GetSelected(action, controller))
                li.AddCssClass("sel");
            li.InnerHtml = a.ToString();

            listBuilder.Append(li.ToString());
        }

        ul.InnerHtml = listBuilder.ToString();

        return ul.ToString();
    }
}

public class SimpleNavItem
{
    public string Text { get; set; }
    public string Action { get; set; }
    public string Controller { get; set; }
    public Func<string, string, bool> GetSelected { get; set; }
}

现在其他3个页面将继承Site.Master 例如About.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">About</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>About</h2>

</asp:Content>

现在我有另一个母版页 NAMED MAIN.MASTER 我希望我的site.master应该从main.master页面继承...那应该是什么

0 个答案:

没有答案