.NET MVC C# - 将类添加到当前菜单项

时间:2015-08-17 16:49:26

标签: c# .net asp.net-mvc

我有一个助手为当前菜单项添加一个类

public static string CurrentItem(this HtmlHelper helper, string action, string controller)
    {
        string classValue = "";

        string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
        string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

        if (currentController == controller && currentAction == action)
        {
            classValue = "wb-navcurr";
        }

        return classValue;
    }

但它没有在我的Partial中工作,因为我已经设置了类......

@Html.ActionLink("Search", "Search", "Home", null, new { @class = "list-group-item wb-sec-h3 h3" })

当我添加我的Helper类时,它只显示在HTML

@Html.ActionLink("Search", "Search", "Home", null, new { @class = "list-group-item wb-sec-h3 h3 Html.CurrentItem(\"Search\", \"Home\")" })

那么如何将课程添加到我已经拥有的课程中呢?

谢谢

1 个答案:

答案 0 :(得分:0)

这只是一个字符串:

"list-group-item wb-sec-h3 h3 Html.CurrentItem(\"Search\", \"Home\")"

它可能包含类似于人类的代码,但.NET不会执行该代码。这只是一个字符串。

为了执行代码,您必须将其从字符串中删除并将其结果连接到文字字符串。可能看起来像这样:

"list-group-item wb-sec-h3 h3 " + Html.CurrentItem("Search", "Home")