我不确定这是一个简单的误解,还是不正确的范例来使用这种功能,但这是我到目前为止想到的。。。
可以!所以。代码在哪里?
我已经实现了下面的Html助手扩展,以帮助从我需要的标准链接创建稍微“调整”的链接。
public static MvcHtmlString ActiveActionLink (this HtmlHelper helper, string labelText, string action, string controller)
{
var cssProprties = controller + " active";
return helper.ActionLink(labelText, action, controller, null, new { @class = cssProprties });
}
我的理论是,我所需要做的就是挖掘正确的对象来定位我当前所在的控制器,并将其与传入的“controller”参数匹配。如果这些匹配,则添加活动类。
所以,有两个问题。。。
如果是这样的话:我可以从我所处的层次挖掘哪些对象来获取我所需要的信息进行比较?
更新:
当我写一个问题的时候,通常发生的事情发生了,所以,我有了一个脑电波。我注意到BeginForm帮助程序中的一块代码导致我创建了以下示例:
public static MvcHtmlString ActiveActionLink (this HtmlHelper helper, string labelText, string action, string controller)
{
var cssProprties = controller;
// if this controller is the target controller, page is active.
if (helper.ViewContext.RouteData.Values["controller"].ToString() == controller)
cssProprties += " active";
return helper.ActionLink(labelText, action, controller, null, new { @class = cssProprties });
}
所以我猜这回答了“我可以挖哪些物体?”问题。同时,这也直接受到MVC源代码的影响,大概是“这是正确的/可以接受的”思维方式吗ASP.NETmvc?“还有问题吗?