3

Is there a way to get access to the full @Url helper object inside an @helper declaration?

I want to do something like this...

@helper Button(System.Web.Mvc.ActionResult action, string text)
{
    <input type="button" name="btn" value="@text" onclick="windows.location='@Url.Action(action)'" />
}

I found this question but this solution does not get you access to @Url.Action(ActionResult). Please note the specific overload that I am looking for.

So has anyone figured out a solution to this one? I could always do the extension method style for custom helpers, but I like the @helper style and I've already run into this problem a few times.

Thanks in advance, everybody!

Community
  • 1
  • 1
jrizzo
  • 1,652
  • 4
  • 17
  • 29

2 Answers2

3

Is there a way to get access to the full @Url helper object inside an @helper declaration?

Yes, by passing it as argument to the helper method.

@helper Button(string action, string text, UrlHelper url)
{
    <input type="button" name="btn" value="@text" onclick="windows.location='@url.Action(action)'" />
}

and when you invoke the helper from some view:

@Button("someAction", "some text", Url)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Great idea, but it does not work, or I am doing something wrong. I tried the following and the only overloads of the action method that I get are the ones that take the action name as a string... `@helper ActionButton(System.Web.Mvc.ActionResult action, string text, System.Web.Mvc.UrlHelper url) { }` – jrizzo Aug 01 '11 at 21:19
  • @jrizzo, what overloads of the Action method that do not take a string that you are talking about? There is no overload that of the UrlHelper.Action method that takes a `System.Web.Mvc.ActionResult` as a first argument that I am aware of. You must be trying something wrong. – Darin Dimitrov Aug 01 '11 at 21:20
  • I'm looking for the overload that takes an `ActionResult`. – jrizzo Aug 01 '11 at 21:21
  • @jrizzo, ???????????? I am afraid that the overload you are looking for simply doesn't exist. Take a look at the documentation: http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action.aspx – Darin Dimitrov Aug 01 '11 at 21:22
  • Hmmm... I can't find the documentation, but if I type `@Url.Action(myActionResult)` in a regular .cshtml page, the _first_ overload in Intellisense takes an `ActionResult`. Maybe It's part of an package I have... I'll get back to you if I figure out which one it is (or let me know if anyone else knows...). – jrizzo Aug 01 '11 at 21:28
0

I am not sure about the Razor, but for html helpers Yes you can. using RequestContext of the helper as shown below. here is one example I wrote for ImageFor html helper:

    public static HtmlString ImageFor(this HtmlHelper helper, string url ) 
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        TagBuilder tb = new TagBuilder("img");
        tb.MergeAttribute("src", urlHelper.Content(url));
        return new HtmlString(tb.ToString(TagRenderMode.SelfClosing)); 
    }
matmat
  • 2,366
  • 5
  • 19
  • 23