我怎样才能返回一个ASP.NET MVC视图当前操作? [英] How can I return the current action in an ASP.NET MVC view?

查看:212
本文介绍了我怎样才能返回一个ASP.NET MVC视图当前操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想成立一​​个CSS类在我的母版页,这取决于电流控制器和行动。我可以通过 ViewContext.Controller.GetType()。名称到电流控制器,但我如何获得当前操作(例如,首页显示等)?

I wanted to set a CSS class in my master page, which depends on the current controller and action. I can get to the current controller via ViewContext.Controller.GetType().Name, but how do I get the current action (e.g. Index, Show etc.)?

推荐答案

使用 ViewContext ,并期待在的RouteData 收集提取两个控制器和动作元素。但我想设置一些数据变量,表示应用程序上下文(例如,编辑模式或错误),而不是控制器/动作降低了您的视图和控制器之间的耦合。

Use the ViewContext and look at the RouteData collection to extract both the controller and action elements. But I think setting some data variable that indicates the application context (e.g., "editmode" or "error") rather than controller/action reduces the coupling between your views and controllers.

这篇关于我怎样才能返回一个ASP.NET MVC视图当前操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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