I have a view that has a list and I want to add a class to a specific list item depending on a variable in the view model.
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
The variable could be anything. Currently it is an integer. Below is what I have now, but I don't think this is the cleanest way to do this.
string[] listClasses = new string[3];
int? selectedListElement= (int?)ViewData["SelectedListElement"];
if(tabNumber.HasValue)
{
tabClasses[tabNumber.Value] = "selected";
}
<li class="@listClasses[0]">List Item 1</li>
<li class="@listClasses[1]">List Item 2</li>
<li class="@listClasses[2]">List Item 3</li>
Why not put the class name into your view model and use conditional attributes