我需要確定我是否處於特定視圖。我的用例是,我想用當前視圖的「on」類裝飾導航元素。有沒有內置的方法來做到這一點?Asp.Net MVC:如何確定您目前是否處於特定視圖
7
A
回答
6
在這裏,我正在使用。我認爲這實際上是由VS中的MVC項目模板生成的:
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;
}
2
我目前的解決方案是擴展方法:
public static class UrlHelperExtensions
{
/// <summary>
/// Determines if the current view equals the specified action
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
/// <param name="helper">Url Helper</param>
/// <param name="action">The action to check.</param>
/// <returns>
/// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
/// </returns>
public static bool IsAction<TController>(this UrlHelper helper, LambdaExpression action) where TController : Controller
{
MethodCallExpression call = action.Body as MethodCallExpression;
if (call == null)
{
throw new ArgumentException("Expression must be a method call", "action");
}
return (call.Method.Name.Equals(helper.ViewContext.ViewName, StringComparison.OrdinalIgnoreCase) &&
typeof(TController) == helper.ViewContext.Controller.GetType());
}
/// <summary>
/// Determines if the current view equals the specified action
/// </summary>
/// <param name="helper">Url Helper</param>
/// <param name="actionName">Name of the action.</param>
/// <returns>
/// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
/// </returns>
public static bool IsAction(this UrlHelper helper, string actionName)
{
if (String.IsNullOrEmpty(actionName))
{
throw new ArgumentException("Please specify the name of the action", "actionName");
}
string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller");
return IsAction(helper, actionName, controllerName);
}
/// <summary>
/// Determines if the current view equals the specified action
/// </summary>
/// <param name="helper">Url Helper</param>
/// <param name="actionName">Name of the action.</param>
/// <param name="controllerName">Name of the controller.</param>
/// <returns>
/// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
/// </returns>
public static bool IsAction(this UrlHelper helper, string actionName, string controllerName)
{
if (String.IsNullOrEmpty(actionName))
{
throw new ArgumentException("Please specify the name of the action", "actionName");
}
if (String.IsNullOrEmpty(controllerName))
{
throw new ArgumentException("Please specify the name of the controller", "controllerName");
}
if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
{
controllerName = controllerName + "Controller";
}
bool isOnView = helper.ViewContext.ViewName.SafeEquals(actionName, StringComparison.OrdinalIgnoreCase);
return isOnView && helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
}
}
1
這裏是東西有點不同,使用FilterAttribute:
[NavigationLocationFilter("Products")]
public ViewResult List()
{
return View();
}
...
public class NavigationLocationFilterAttribute : ActionFilterAttribute
{
public string CurrentLocation { get; set; }
public NavigationLocationFilterAttribute(string currentLocation)
{
CurrentLocation = currentLocation;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = (Controller)filterContext.Controller;
controller.ViewData.Add("NavigationLocation", CurrentLocation);
}
}
...
A nd在視圖中:
<%= ViewData["NavigationLocation"] %>
相關問題
- 1. 如何確定ASP.NET MVC 3中是否存在視圖?
- 2. 確定當前視圖是否爲特定活動
- 3. 確定MVC視圖是否嵌套
- 4. 確定Asp.net MVC中特定頁面的視圖計數
- 5. 如何確定視圖是用於ASP.NET MVC中的GET還是POST?
- 6. 如何檢查特定UIViewController的視圖當前是否可見?
- 7. ASP.NET MVC ActionFilter - 確定是否AJAX請求
- 8. 確定用戶是否處於後臺的特定位置
- 9. 如何檢查一個特定的視圖目前是否集中在Android
- 10. 使用Asp.net MVC確定類似於YouTube的視圖計數
- 11. 在ASP.NET MVC中確定部分視圖中當前視圖的名稱
- 12. 如何使用ASP.net將對象傳遞到特定視圖MVC
- 13. 如何確定自定義視圖當前是否在ViewPager中可見
- 14. 如何在Asp.Net MVC中確定剃鬚刀視圖中的「IsPartial」?
- 15. ASP.Net MVC如何確定從控制器使用的視圖?
- 16. 如何確定ASP.NET MVC 1.0驗證視圖內的狀態
- 17. 如何確定圖像是否包含特定的顏色?
- 18. 如何確定viewflipper的當前視圖?
- 19. iOS5如何確定當前視圖
- 20. ASP.Net MVC如何確定用戶是否可以訪問URL?
- 21. 如何確定ASP.NET MVC是否找到了Action參數的值
- 22. 如何確定編譯器目前處於ARC模式?
- 23. 用戶特定視圖MVC
- 24. 有什麼選擇可以確定我的ASP.NET MVC視圖是否符合XHTML
- 25. 是否有可能以編程方式確定ASP.NET MVC的Razor視圖模型?
- 26. 如何刪除ASP.NET C#中的特定列表視圖項目?
- 27. 確定點是否位於特定幾何區域內
- 28. 如何在ASP.NET MVC 3中創建特定於特定角色/用戶的視圖和控制器文件夾?
- 29. 確定是否應使用.NET MVC
- 30. Youtube-API:如何確定我之前是否喜歡過視頻?