我想在我的母版页中设置一个CSS类,这取决于当前控制器和动作。我可以通过ViewContext.Controller.GetType()访问当前控制器。名称,但我如何得到当前的行动(如索引,显示等)?
当前回答
在控制器中重写此函数
protected override void HandleUnknownAction(string actionName)
{ TempData["actionName"] = actionName;
View("urViewName").ExecuteResult(this.ControllerContext);
}
其他回答
我投票给这个2:
string currentActionName = ViewContext.RouteData.GetRequiredString("action");
and
string currentViewName = ((WebFormView)ViewContext.View).ViewPath;
您可以检索当前视图的物理名称和触发该视图的操作。它可以在partial *中使用。Acmx页来确定主机容器。
我看到了不同的答案,想出了一个类助手:
using System;
using System.Web.Mvc;
namespace MyMvcApp.Helpers {
public class LocationHelper {
public static bool IsCurrentControllerAndAction(string controllerName, string actionName, ViewContext viewContext) {
bool result = false;
string normalizedControllerName = controllerName.EndsWith("Controller") ? controllerName : String.Format("{0}Controller", controllerName);
if(viewContext == null) return false;
if(String.IsNullOrEmpty(actionName)) return false;
if (viewContext.Controller.GetType().Name.Equals(normalizedControllerName, StringComparison.InvariantCultureIgnoreCase) &&
viewContext.Controller.ValueProvider.GetValue("action").AttemptedValue.Equals(actionName, StringComparison.InvariantCultureIgnoreCase)) {
result = true;
}
return result;
}
}
}
所以在视图(或master/layout)中,你可以这样使用它(Razor语法):
<div id="menucontainer">
<ul id="menu">
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("home", "index", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("Home", "Index", "Home")</li>
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("account","logon", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("Logon", "Logon", "Account")</li>
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("home","about", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
希望能有所帮助。
你可以从ViewContext的RouteData中获取这些数据
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]
我知道这是一个老问题,但我看到了它,我认为您可能对一个替代版本感兴趣,而不是让您的视图处理检索它需要的数据。
在我看来,一个更简单的方法是重写onactionexecution方法。你被传递一个ActionExecutingContext,它包含ActionDescriptor成员,你可以用它来获取你正在寻找的信息,也就是ActionName,你也可以到达ControllerDescriptor,它包含ControllerName。
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
string actionName = actionDescriptor.ActionName;
string controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
// Now that you have the values, set them somewhere and pass them down with your ViewModel
// This will keep your view cleaner and the controller will take care of everything that the view needs to do it's job.
}
希望这能有所帮助。如果有什么不同的话,至少它会为其他问你问题的人提供一种选择。
获取视图的当前Id:
ViewContext.RouteData.Values["id"].ToString()
获取当前控制器。
ViewContext.RouteData.Values["controller"].ToString()
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- ASP中基于角色的访问控制(RBAC)与基于声明的访问控制(CBAC)NET MVC
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- 带有空模型的Renderpartial传递了错误的类型
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- MVC4数据类型。日期编辑器不会在Chrome中显示日期值,在Internet Explorer中没问题
- 如何将此foreach代码转换为Parallel.ForEach?