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.
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!