views:

178

answers:

1

Hi, I'm trying out ASP.NET MVC Framework and would like to create an ajax helper method. Basically this helper would act like ActionLink but without encoding its link text. It is rather trivial to write as an HtmlHelper, you simply have to write your own version of GenerateLinkInternal. This isn't working for AjaxHelpers though, as the ajax version of GenerateLink is indirectly calling ToJavascriptString (through GenerateAjaxScript) which is internal, thus cannot be called outside the MVC assembly. I sure can rewrite the whole thing, but it seems way overkill, is there a better way?

Ultimately, I'd like to make this helper act like BeginForm to make the link surround a block of HTML. I've not looked at it yet, but I assume that it uses ToJavascriptString too. I've searched the web and, looking through the MVC source code, I begin to wonder if I'm completely on the wrong track.

Thanks

Update: The more I look at this problem, the more I think that there's simply no solution. Whoever wrote the MVC Framework didn't think about helping people write their own helpers!

Update: I've ended up writing an helper that pretty much duplicate AjaxOptions functionality.

A: 

You could probably do this a lot easier by writing your own helper from scratch (i.e. don't make calls to any of the Html.ActionLink()/Ajax.ActionLink() methods) simply by using Url.Action() instead.

For example, it's pretty trivial to do this:

public static string NonEncodedUrl(this HtmlHelper helper, 
    string linkAction, string text)
{
    // Get a new UrlHelper instance in the current context
    var url = new UrlHelper(helper.ViewContext.RequestContext);

    return String.Format("<a href=""{0}"">{1}</a>", url.Action(linkAction), text);
}

You can of course extend this with overloads and extra parameters to suit your own needs.

Tomas Lycken
Which amount to rewriting everything! Then I would need to include a copy of AjaxOptions in my code or rewrite the same functionality another way.
Nicolas Buduroi