我怎样才能返回一个ASP.NET MVC视图当前操作? [英] How can I return the current action in an ASP.NET MVC view?
问题描述
我想成立一个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 code >收集提取两个控制器和动作元素。但我想设置一些数据变量,表示应用程序上下文(例如,编辑模式或错误),而不是控制器/动作降低了您的视图和控制器之间的耦合。
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屋!