Skip to content

Instantly share code, notes, and snippets.

@SyntaxC4
Created January 1, 2013 01:04
Show Gist options
  • Select an option

  • Save SyntaxC4/4424427 to your computer and use it in GitHub Desktop.

Select an option

Save SyntaxC4/4424427 to your computer and use it in GitHub Desktop.
namespace Net.SyntaxC4.Blog.Examples
{
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
public static class HtmlExtensions
{
public static MvcHtmlString ActionLinkWithSpan(this HtmlHelper html,
string linkText,
string actionName,
string controllerName,
object htmlAttributes)
{
RouteValueDictionary attributes = new RouteValueDictionary(htmlAttributes);
TagBuilder linkTag = new TagBuilder("a");
TagBuilder spanTag = new TagBuilder("span");
spanTag.SetInnerText(linkText);
// Merge Attributes on the Tag you wish the htmlAttributes to be rendered on.
//linkTag.MergeAttributes(attributes);
spanTag.MergeAttributes(attributes);
UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);
linkTag.Attributes.Add("href", url.Action(actionName, controllerName));
linkTag.InnerHtml = spanTag.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.Normal));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment