ASP.NET MVC - Ajax.ActionLink with HtmlString

Is it possible to use the Ajax.ActionLink method with an HtmlString as link text and not a string?

The reason for this is that I want to do something like this:

@Ajax.ActionLink("<span style='highlight'>Hello</span> World", ...) 

If I try this now, it will return a span, etc. on my page. So I need to return it as an HtmlString

EDIT:

Just tried:

  public static MvcHtmlString ActionLink( this AjaxHelper ajaxHelper, IHtmlString linkText, string actionName, Object routeValues, AjaxOptions ajaxOptions ) { return ActionLink(ajaxHelper, linkText, actionName, routeValues, ajaxOptions); } 

And I got an exception?

+4
source share
2 answers
+4
source

No, I do not believe. ASP.NET MVC will encode HTML text so that it displays as such. What you need to use is an overload of the AjaxExtensions.ActionLink() method. Something like this will work:

AjaxExtenions.ActionLink ()

Method parameter list:

 public static MvcHtmlString ActionLink( this AjaxHelper ajaxHelper, string linkText, string actionName, Object routeValues, AjaxOptions ajaxOptions, Object htmlAttributes ) 

Note that there are HTML Object attributes. But this will only be for the HTML attributes of this anchor element. If you are really using the <span /> element, it is best to write HtmlHelper for it.

Example

 @Ajax.ActionLink( "This is your link text", "SomeActionName", null, // or you could pass route values if you like new AjaxOptions() { }, // whatever Ajax options you want null); // not passing any html attributers 
0
source

All Articles