Asp.Net MVC:如何确定如果您目前是在一个特定视图 [英] Asp.Net MVC: How to determine if you're currently on a specific view

查看:84
本文介绍了Asp.Net MVC:如何确定如果您目前是在一个特定视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定,如果我在一个特定的视图。我使用的情况是,我想装点导航元素与开类的观点。是否有一个内置在做这个的方法是什么?

I need to determine if I'm on a particular view. My use case is that I'd like to decorate navigation elements with an "on" class for the current view. Is there a built in way of doing this?

推荐答案

我在这里使用的是什么。我认为这实际上是在VS的MVC项目模板生成的:

Here what i am using. I think this is actually generated by the MVC project template in VS:

public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
    {
        string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            return true;

        return false;
    }

这篇关于Asp.Net MVC:如何确定如果您目前是在一个特定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆