I started moving logic out of my Razor Views and into HTML Helper classes.
Here's my first, and it's a beauty (uses recursion to build out nested lists):
public static class CategoryExtensions { public static IHtmlString CategoryDisplay(this HtmlHelper helper, IEnumerableI'm glad to get the logic out of the Razor views (which should not have such complex logic) and into proper C# classes.categories, int indentSize) { string indentString = ""; for (int i = 0; i < indentSize; i++) { indentString += " "; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" \n"); stringBuilder.Append(BuildList(categories, indentString)); stringBuilder.Append(indentString).Append(""); return new HtmlString(stringBuilder.ToString()); } private static IHtmlString BuildList(IEnumerablecategories, string indentString) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(indentString).Append(" \n"); foreach (Category category in categories) { stringBuilder.Append(indentString).Append("
\n"); return new HtmlString(stringBuilder.ToString()); } }- \n"); stringBuilder.Append(indentString).Append("
\n"); } stringBuilder.Append(indentString).Append("\n"); stringBuilder.Append(indentString).Append(" " + category.CategoryName + "\n"); stringBuilder.Append(indentString).Append("\n"); if (category.SubCategories.Any()) { indentString += " "; stringBuilder.Append(BuildList(category.SubCategories, indentString)); } stringBuilder.Append(indentString).Append("
No comments:
Post a Comment