Hey I would love to know what you think while reading my posts. Please comment!.

MVC ActionLink with HTML Content

The Situation

You are wanting to utilise either Html.ActionLink or Ajax.ActionLink, but you want the link to contain HTML content. The "out of the box" helpers take in a String for the link text, and if you were to add in HTML here, it would work, however the HTML would be rendered as a string on the page.

The Solution

Create your own Helper Extension which returns a MvcHtmlString

public static class HtmlHelperExtensions
{
 
    public static MvcHtmlString RawActionLink(this AjaxHelper ajaxHelper, string rawHtml, string action, string controller, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
    {
        //string anchor = ajaxHelper.ActionLink("##holder##", action, controller, routeValues, ajaxOptions, htmlAttributes).ToString();
        //return MvcHtmlString.Create(anchor.Replace("##holder##", rawHtml));
        /* Updated code to use a generated guid as from the suggestion of Phillip Haydon */
        string holder = Guid.NewGuid().ToString();
        string anchor = ajaxHelper.ActionLink(holder, action, controller, routeValues, ajaxOptions, htmlAttributes).ToString();
        return MvcHtmlString.Create(anchor.Replace(holder, rawHtml));
    }
 
    public static MvcHtmlString RawActionLink(this HtmlHelper htmlHelper, string rawHtml, string action, string controller, object routeValues, object htmlAttributes)
    {
        //string anchor = htmlHelper.ActionLink("##holder##", action, controller, routeValues, htmlAttributes).ToString();
        //return MvcHtmlString.Create(anchor.Replace("##holder##", rawHtml));
        /* Updated code to use a generated guid as from the suggestion of Phillip Haydon */
        string holder = Guid.NewGuid().ToString();
        string anchor = htmlHelper.ActionLink(holder, action, controller, routeValues, htmlAttributes).ToString();
        return MvcHtmlString.Create(anchor.Replace(holder, rawHtml));
    }
 
}

Then you can create both Html.RawActionLink and Ajax.RawActionLink

@Ajax.RawActionLink("<h1>Test</h1>", "Index", "Home", null, null, null)
@Html.RawActionLink("<h2>Test</h2>", "Index", "Home", null, null)

Although, having header tags within anchor tags isn't great ;)

Working example of on Github

UPDATED - Using a generated Guid as a placeholder as per the suggestion from Phillip Heydon

PLEASE LEAVE COMMENTS! I am always interested in hearing back from those who read my blog. Please leave a comment if you found this useful, want to suggest any changes to my code, or anything else! Thanks!

About Me

Tim James I'm Tim, a web applications developer from Glasgow, Scotland. Currently working for Kingfisher Systems Ltd, building bespoke systems within the Car Auction industry.

  • C#
  • VB.NET
  • ASP.NET
  • .NET MVC
  • Web API
  • Razor
  • HTML5
  • CSS3
  • jQuery
  • WCF
  • SQL
  • knockout.js
  • Angularjs
  • AJAX
  • APIs
  • SignalR
Why not follow me on twitter? Follow me on Twitter