2011-07-09 75 views
0

對於使用不是最好的imo的Html幫助器方法的設置,因爲我使用靜態字段。在菜單中設置當前,當前和當前元素之後

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禁用。

+0

它是無用的,我不知道靜態是相同的每個網站的實例... – mrzepa

回答

0

我終於解決了在一個幫手生成整個菜單:

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; 
} 

,並考慮:

@{ 
    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> 
    } 
} 

並不完美,但它的作品至少。