特定のビューにいるかどうかを判断する必要があります。私の使用例は、現在のビューの "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ビューの中で:
<%= ViewData["NavigationLocation"] %>
関連する問題
- 1. 現在のビューが特定のアクティビティであるかどうかを確認する
- 2. 現在のURLが特定のパターンで終了するかどうかを確認する方法
- 3. 現在アクティビティインスタンスが存在するかどうかを確認する方法
- 4. ASP.NET MVCの部分ビュー内の現在のビューの名前を確認
- 5. tabbarcontrollerの現在アクティブなビューを確認する方法
- 6. NSTableViewの特定のビューにマウスがあるかどうかを確認する方法はありますか?
- 7. ルート(ASP.NET MVC)が特定のパスに存在するかどうかを確認するにはどうすればよいですか?
- 8. データベースに特定の日付がすでに存在するかどうかを確認する方法
- 9. ASP.NET MVC - 現在のアクションを部分ビューから取得する方法
- 10. iOS現在電話中かどうかを確認する方法
- 11. 特定のXML要素がXML文書に存在するかどうかを確認する方法
- 12. * .chmファイルに特定のトピックが存在するかどうかを確認する方法は?
- 13. 現在のコンテキストがjQueryスタックかどうかを確認する
- 14. ファイルが特定のディレクトリに存在するかどうかの確認
- 15. 特定の要素がすべて存在するかどうかを確認する方法
- 16. 特定のアセンブリが存在するかどうかを確認する方法は?
- 17. MVC、動的ルーティング、Unity、現在のコントローラがglobal.asaxに存在するかどうかを確認
- 18. 特定のNSStringがNSArrayに存在するかどうかをどのように確認できますか?
- 19. 現在のブラウザにC#コードがロードされているかどうかを確認する方法
- 20. ユーザーが現在オンラインかどうか確認する(ライブ)
- 21. ASP.NET MVCビューでページタイトルを設定するにはどうすればよいですか? ASP.NET MVCのビューで
- 22. Asp.Net MVC:現在のコントローラ/ビューの仮想URLを取得するにはどうすればよいですか?
- 23. ASP.NET MVC - ビューの現在のフォルダ
- 24. 特定のウィンドウが現在移動中であるかどうかを確認します
- 25. ウィンドウが現在のデスクトップにあるかどうかを確認しますか?
- 26. 現在の座標が他の座標の半径内にあるかどうかを確認する方法
- 27. データベースがSQL Serverに存在するかどうかを確認する方法?
- 28. windowsstore appにファイルが存在するかどうかを確認する方法
- 29. Selenium - リストボックスにテキストが存在するかどうかを確認する方法
- 30. アイテムがCache(System.Web.Cache)に存在するかどうかを確認する方法?