Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created November 11, 2015 20:01
Show Gist options
  • Select an option

  • Save jbnv/c6bc0648e7deea73c00a to your computer and use it in GitHub Desktop.

Select an option

Save jbnv/c6bc0648e7deea73c00a to your computer and use it in GitHub Desktop.
Menu HTML helper for ASP.Net MVC.
// Untested!
public class MenuItem
{
public bool Show { get; set; }
public MvcHtmlString Html { get; set; }
}
public class Menu
{
public bool Show { get; set; }
public string IconClass { get; set; }
public string Title { get; set; }
public IList<MenuItem> Items { get; set; }
}
public static class MenuHtmlHelper
{
public static MvcHtmlString Menu(this HtmlHelper helper, Menu menu)
{
var sb = new StringBuilder();
if (menu.Show)
{
sb.AppendLine("<li class=\"dropdown\">");
sb.Append("<a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\"> <i class=\"fa ");
sb.Append(menu.IconClass);
sb.Append("\"></i> ");
sb.Append(menu.Title);
sb.AppendLine(" <b class=\"caret\"></b></a>");
sb.AppendLine("<ul class=\"dropdown-menu\">");
foreach (var item in menu.Items.Where(item => item.Show))
{
sb.Append("<li class=\"dropdown\">");
sb.Append(item.Html);
sb.AppendLine("</li>");
}
sb.AppendLine("</ul>");
sb.AppendLine("</li>");
}
return new MvcHtmlString(sb.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment