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

Go To StackoverFlow.com

0

In my controller I use the following:

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

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

@Html.DropDownList("ReferenceID", 
   null, 
   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>
</ul>

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


1

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: http://aspnet.codeplex.com - Dmitry Reznik 2012-04-04 06:38


1

Write your HtmlHelper http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs but the best ways is to browse MVC source code and learn http://aspnet.codeplex.com/SourceControl/changeset/view/72551#288014 or use a foreach.

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

</ul>

Regards

2012-04-04 06:36
by Matija Grcic
Ads