Is there an equivalent to HTML.DropDownList that creates an unordered list in MVC3?

Go To


In my controller I use the following:

ViewBag.ReferenceId = new SelectList(_reference.Get("00")
   .OrderBy(o => o.Order), "RowKey", "Value", "00");

Then in my view I use the following extension to create a dropdown:

   new { 
     id = "ReferenceID", 
     @class = "combobox2", 
     style="width: 150px;"

Now I would like to change this to create an unordered list. Something like the following:

<ul id="categories" class="pane list">
   <li><a href="#">Websites</a></li>
   <li><a href="#">Web Design</a></li>
   <li><a href="#">Print</a></li>

Is there any MVC extension that can do this for me or is there an easy way I can code something?

2012-04-04 06:03
by Samantha J T Star
Why not just a @for-each - McGarnagle 2012-04-04 06:08


There is no equivalent, though you could write it on your own like this:

tagsList = "<ul>{0}</ul>";
tagsListItem = "<li>{0}</li>";
StringBuilder result = new StringBuilder();
list.ForEach(listItem => result.AppendFormat(tagsListItem, item.ToString()));
return string.Format(tagsList, result.ToString());

And you could fit the code for your needs (like adding additional tags, attributes etc.)

2012-04-04 06:20
by Dmitry Reznik
This seems like a good idea but I am not sure about how to write the extension. Can you tell me where I can find the code for @Html.DropDownList so maybe I can adopt that as a starting point. Thank - Samantha J T Star 2012-04-04 06:29
Source can be found here: - Dmitry Reznik 2012-04-04 06:38


Write your HtmlHelper but the best ways is to browse MVC source code and learn or use a foreach.

<ul id="categories" class="pane list">
@foreach(var category in categories)
  <li><a href="#">@category</a></li>



2012-04-04 06:36
by Matija Grcic