Hi All,
Quick question.
How would you refactor this Asp.net MVC 2 HtmlHelper? Specifically would it make sense to use the TagBuilder class in this scenario?
public static MvcHtmlString BusinessDisplayContacts(this HtmlHelper helper, string phone, string cellPhone,
string fax, string website, string email, bool hideEmail)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<ul>");
if (!string.IsNullOrEmpty(phone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Work</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",phone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(cellPhone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Cell</span> Phone:");
sb.AppendFormat("<span class=\"value\">{0}</span>",cellPhone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(fax)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Fax</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",fax);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(website)) {
sb.AppendFormat("<li><a class=\"url\" href=\"{0}\">{0}</a></li>",website);
}
if (!hideEmail && !string.IsNullOrEmpty(email)) {
sb.AppendFormat("<li><a class=\"email\" href=\"mailto:{0}\">{0}</a></li>",email);
}
sb.AppendLine("</ul>");
if (sb.Length < 10)
{
return MvcHtmlString.Create("");
}
else {
return MvcHtmlString.Create(sb.ToString());
}
}
Thanks in advance.
UPDATE:
Thank for all of the constructive comments.
In the end I've decided to move the above code into a strongly typed partial view as per @queen3's suggestion.