I'd like to add an HtmlHelper extension method to create an unordered list, and I'd prefer to add in attributes the same way the existing HtmlHelpers do. I'm creating some overloads, and this example creates a list from a List<SelectListItem> collection, and places anchor tags inside each list item:
I'd much rather pass the attributes in as a collection, and add them in via a loop. I've seen an example somewhere on how to do this, but can't recall where it is. I'd sooner not use the TagBuilder class, unless the performance issues I've read about are
exaggerated. Some Advice?
I saw this tutorial, and at its most basic it does demonstrate using the TagBuilder class. However, I'm wondering if it's as efficient as building the HTML via a StringBuilder, and what happens if you try to fetch an attribute that doesn't exist. I'll play
around with it a bit, but I'm also wondering if anyone has used it to create unordered lists or something similar.
public static MvcHtmlString UnorderedList(this HtmlHelper helper,
string id, List<SelectListItem> items, string classname = "",
IDictionary<string, object> htmlAttributes = null)
{
var attributes = new StringBuilder();
if (htmlAttributes != null)
{
foreach (var htmlAttribute in htmlAttributes)
{
attributes.Append(htmlAttribute);
}
}
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<ul id='{0}',"+ attributes +">", id);
foreach (SelectListItem item in items)
sb.AppendFormat("<li><a href='#' id='{0}' class='{1}'>{2}</a></li>", item.Value, classname, item.Text);
sb.AppendLine("</ul>");
return MvcHtmlString.Create(sb.ToString());
}
Hope it helps
Best regards
Khashayar
---------------------------------------------
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
Ironwil
Member
76 Points
96 Posts
How to add HTML attributes to custom HtmlHelper extension
Nov 13, 2012 04:37 PM|LINK
I've made some UrlHelper method extensions, but mostly they're just very simple methods to provide a shorthand for paths, like this:
public static MvcHtmlString JS(this UrlHelper urlHelper, string filename) { string url = urlHelper.Content("~/js/{0}".FormatWith(filename)); return MvcHtmlString.Create(@"<script src='{0}' type='text/javascript'></script>".FormatWith(url)); }I'd like to add an HtmlHelper extension method to create an unordered list, and I'd prefer to add in attributes the same way the existing HtmlHelpers do. I'm creating some overloads, and this example creates a list from a List<SelectListItem> collection, and places anchor tags inside each list item:
public static MvcHtmlString UnorderedList(this HtmlHelper helper, string id, List<SelectListItem> items, string classname = "") { StringBuilder sb = new StringBuilder(); sb.AppendFormat("<ul id='{0}'>", id); foreach (SelectListItem item in items) sb.AppendFormat("<li><a href='#' id='{0}' class='{1}'>{2}</a></li>", item.Value, classname, item.Text); sb.AppendLine("</ul>"); return MvcHtmlString.Create(sb.ToString()); }I'd much rather pass the attributes in as a collection, and add them in via a loop. I've seen an example somewhere on how to do this, but can't recall where it is. I'd sooner not use the TagBuilder class, unless the performance issues I've read about are exaggerated. Some Advice?
CPrakash82
All-Star
18720 Points
2899 Posts
Re: How to add HTML attributes to custom HtmlHelper extension
Nov 13, 2012 06:11 PM|LINK
Its given here.
http://www.asp.net/mvc/tutorials/older-versions/views/using-the-tagbuilder-class-to-build-html-helpers-cs
Ironwil
Member
76 Points
96 Posts
Re: How to add HTML attributes to custom HtmlHelper extension
Nov 13, 2012 06:36 PM|LINK
I saw this tutorial, and at its most basic it does demonstrate using the TagBuilder class. However, I'm wondering if it's as efficient as building the HTML via a StringBuilder, and what happens if you try to fetch an attribute that doesn't exist. I'll play around with it a bit, but I'm also wondering if anyone has used it to create unordered lists or something similar.
khparhami
Member
447 Points
194 Posts
Re: How to add HTML attributes to custom HtmlHelper extension
Nov 15, 2012 02:29 AM|LINK
Hi,
Try this
public static MvcHtmlString UnorderedList(this HtmlHelper helper, string id, List<SelectListItem> items, string classname = "", IDictionary<string, object> htmlAttributes = null) { var attributes = new StringBuilder(); if (htmlAttributes != null) { foreach (var htmlAttribute in htmlAttributes) { attributes.Append(htmlAttribute); } } StringBuilder sb = new StringBuilder(); sb.AppendFormat("<ul id='{0}',"+ attributes +">", id); foreach (SelectListItem item in items) sb.AppendFormat("<li><a href='#' id='{0}' class='{1}'>{2}</a></li>", item.Value, classname, item.Text); sb.AppendLine("</ul>"); return MvcHtmlString.Create(sb.ToString()); }Hope it helps
Khashayar
---------------------------------------------
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.