Mvc Helpers – HtmlAttributes Extension
Just a quick not on how to pass and render HtmlAttributes in a helper:
public HtmlString Add(YourObject yourObject, object htmlAttributes = null) { var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); //process you markup etc. attributes.ToHtmlAttributes() } public static class RouteValueDictionaryExtensions { public static IHtmlString ToHtmlAttributes(this RouteValueDictionary dictionary) { var sb = new StringBuilder(); foreach (var kvp in dictionary) { sb.Append(string.Format("{0}=\"{1}\" ", kvp.Key, kvp.Value)); } return new HtmlString(sb.ToString()); } public static void ToHtmlAttributes(this RouteValueDictionary dictionary, StringBuilder sb) { foreach (var kvp in dictionary) { sb.Append(string.Format("{0}=\"{1}\" ", kvp.Key, kvp.Value)); } } }
Leave a Reply