タブ ロジックを含む Site.Masterという名前のマスター ページが 1 つあります。
<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>
タブロジッククラスファイルのコードはこちら
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 For Example 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 ページから継承する必要があります。