bug*_*ggs 290 c# asp.net-mvc
我想在我的母版页中设置一个CSS类,这取决于当前的控制器和操作.我可以通过当前的控制器ViewContext.Controller.GetType().Name,但是如何获得当前的操作(例如Index,Show等等)?
Chr*_*ger 465
在RC中,您还可以像这样提取路径数据,如操作方法名称
ViewContext.Controller.ValueProvider["action"].RawValue
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["id"].RawValue
Run Code Online (Sandbox Code Playgroud)
ViewContext.Controller.ValueProvider.GetValue("action").RawValue
ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("id").RawValue
Run Code Online (Sandbox Code Playgroud)
ViewContext.Controller.RouteData.Values["action"]
ViewContext.Controller.RouteData.Values["controller"]
ViewContext.Controller.RouteData.Values["id"]
Run Code Online (Sandbox Code Playgroud)
ViewContext.RouteData.Values["action"]
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["id"]
Run Code Online (Sandbox Code Playgroud)
tva*_*son 83
使用ViewContext和查看RouteData集合来提取控制器和操作元素.但我认为设置一些指示应用程序上下文的数据变量(例如,"editmode"或"error")而不是控制器/操作会减少视图和控制器之间的耦合.
tsq*_*rio 60
要获取视图上的当前ID:
ViewContext.RouteData.Values["id"].ToString()
Run Code Online (Sandbox Code Playgroud)
要获得当前的控制器:
ViewContext.RouteData.Values["controller"].ToString()
Run Code Online (Sandbox Code Playgroud)
Dal*_*gan 41
我知道这是一个较旧的问题,但是我看到它并且我认为您可能对替代版本感兴趣,而不是让您的视图处理检索它所需的数据来完成它的工作.
在我看来,更简单的方法是覆盖OnActionExecuting方法.您将传递包含ActionDescriptor成员的ActionExecutingContext,您可以使用该成员获取您要查找的信息,即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.
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.如果有的话,至少它会为你的问题所带来的任何其他人展示另一种选择.
Mic*_*sky 17
我看到了不同的答案,并想出了一个班助手:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以在View(或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>
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
Via*_*ukh 14
您可以从ViewContext的RouteData获取这些数据
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]
Run Code Online (Sandbox Code Playgroud)
在MVC中,您应该为View提供所有数据,而不是让View收集自己的数据,因此您可以在控制器操作中设置CSS类.
ViewData["CssClass"] = "bold";
Run Code Online (Sandbox Code Playgroud)
并从View中的ViewData中选择此值
小智 6
我投票支持这个2:
string currentActionName = ViewContext.RouteData.GetRequiredString("action");
Run Code Online (Sandbox Code Playgroud)
和
string currentViewName = ((WebFormView)ViewContext.View).ViewPath;
Run Code Online (Sandbox Code Playgroud)
您可以检索当前视图的物理名称和触发它的操作.它可以在部分*.acmx页面中用于确定主机容器.
| 归档时间: |
|
| 查看次数: |
146502 次 |
| 最近记录: |