1

In my routing I have

routes.MapRoute(
    name: "ctrl",
    url: "ctrl/{target_name}",
    defaults: new { controller = "ctrl", action = "TheAction" }
);

How do I access target_name? I tried ViewBag.target_name, I Googled and found many pages say RouteData.Values but I get a compile error

CS0120: An object reference is required for the non-static field, method, or property 'System.Web.Routing.RouteData.Values.get'

So I end up doing

public ActionResult TheAction(string target_name) { ViewBag.target_name = target_name; return View(); }

which appears to be very wrong (because it seems stupid)

How do I access target_name in my view?

1
  • Is route "ctrl" placed above default route definition in global.asax? And how do you exacly try to call RouteData.Values in cshtml? Commented Mar 17, 2014 at 21:40

1 Answer 1

4

If you want to get at the RouteData in your view you need to get it through the ViewContext.

ViewContext.RouteData.Values["target_name"]
Sign up to request clarification or add additional context in comments.

1 Comment

Why couldn't any of the pages mention or use ViewContext in their example. Thanks that solved it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.