0
静的なフィールドを使うので、私は最高のものではないヘルパーメソッドを使用するように設定します。メニューの現在の要素と現在の要素との後ろに現在の要素を設定します。
public enum CurrentState
{
BeforeCurrent,
AfterCurrent
}
public static CurrentState currentState = CurrentState.BeforeCurrent;
public static MvcHtmlString ActiveActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, bool checkAction = true)
{
string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
if ((controllerName == currentController) && checkAction && (actionName == "Index"))
{
currentState = CurrentState.BeforeCurrent;
}
if ((controllerName == currentController) && checkAction && (actionName != currentAction))
{
if (currentState == CurrentState.BeforeCurrent)
{
return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "beforeCurrent" });
}
else if (currentState == CurrentState.AfterCurrent)
{
return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "afterCurrent" });
}
}
if ((controllerName == currentController) && (!checkAction || (actionName == currentAction)))
{
currentState = CurrentState.AfterCurrent;
return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "current" });
}
return helper.ActionLink(linkText, actionName, controllerName);
}
私は、メニューの二つのレベルを持っていると私はcheckActionパラメータを使用する理由です:
- メインメニュー - @ Html.ActiveActionLink(Resources.Global.mainMenuBoard、 "インデックス"、 "ボード"、checkActionを:偽)
- サイドメニュー - @ Html.ActiveActionLink(Resources.Global.managementOverview @、「インデックス」、「管理」)
とサイドメニューで私はそれが現在の後と前だかどうかを知る必要があり(重複する項目...)。
これを改善する方法はありますか? また、私はそれにもjavascriptを使うと言わなければなりませんが、javascriptも無効にしなければなりません。
@{
List<MvcHtmlString> actionMenu = Html.SubMenuLinks("Manager", new List<Link>()
{
new Link() { LinkText = "linkText", ActionName = "actionName" },
new Link() { LinkText = "linkText2", ActionName = "actionName2" }
});
}
@section SideMenu
{
@for (int i = 0; i < actionMenu.Count; i++)
{
<li id="[email protected](i)">@actionMenu.ElementAt(i)</li>
}
}
は完璧ではないが、それは、少なくとも作品:
public class Link
{
public string LinkText { get; set; }
public string ActionName { get; set; }
}
public static List<MvcHtmlString> SubMenuLinks(this HtmlHelper helper, string controllerName, List<Link> links)
{
List<MvcHtmlString> menuElements = new List<MvcHtmlString>();
string actualCssClass = "beforeCurrent";
string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
foreach (Link link in links)
{
if (controllerName == currentController && link.ActionName == currentAction)
{
menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = "current" }));
actualCssClass = "afterCurrent";
}
else
{
menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = actualCssClass }));
}
}
return menuElements;
}
とビューでは:
それは役に立たない、私は静的がウェブサイトのすべてのインスタンスのために同じであることを以前知らなかった... – mrzepa