0

My project has a HtmlHelper which generates links (with routeValues) which always direct back to the controller/action which 'spawned' them.

Is it possible to retrieve these values from within the HtmlHelper? i.e without supplying them explicitly. This would work fine....

var url = new UrlHelper(html.ViewContext.RequestContext);
anchorBuilder.MergeAttribute("href", url.Action("Details", routeValues));

...were it not for the fact that that action won't always be "Details".

Guy Norton
  • 135
  • 1
  • 10
  • possible duplicate of [How can I return the current action in an ASP.NET MVC view?](http://stackoverflow.com/questions/362514/how-can-i-return-the-current-action-in-an-asp-net-mvc-view) – CodeCaster Sep 15 '14 at 10:25

2 Answers2

1

Or this:

        var controller = helper.ViewContext.RouteData.Values["Controller"].ToString();
        var action = helper.ViewContext.RouteData.Values["Action"].ToString();
StuTheDog
  • 451
  • 1
  • 11
  • 19
0

Try this:

var currentAction = html.ViewContext.RouteData.GetRequiredString("action");
var url = new UrlHelper(html.ViewContext.RequestContext);
anchorBuilder.MergeAttribute("href", url.Action(currentAction, routeValues));
Mindless
  • 2,352
  • 3
  • 27
  • 51