user6950100 user6950100 - 1 month ago 7
HTML Question

How to use DisplayFor to display a list of hrefs

I have an MVC project that has a list of items. I use

@Html.DisplayFor(m => m.ListOfItems)


when running, this shows the list of items. In the markup, it is displayed like this:

item1item2item3item4


But I want them in an
ul
so that I can make them hrefs. How do I do this?

Answer

simple, use a foreach loop:

<ul>
@foreach(var item in Model.ListOfItems)
{
    <li><a href="@Html.DisplayFor(m => item)">Some text</a></li>
}
</ul>