Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Creating two unordered lists while looping through a Dictionary

I am trying to create two Unordered Lists based on the Data from a Dictionary. What I want is when the loop reaches the half of the Dictionary I want it to close the first <ul> and start another.

This is what I have tried but it does not work:

<ul>
    @foreach( var productData in products )
    {
        if(products.ElementAt(products.Count / 2).Key != productData.Key)
        {
            </ul>
            <ul>
        }
        <li><span>@productData.Key</span> : <label>@productData.Value</label></li>
    }
</ul>

How can I solve this problem?

Upvotes: 0

Views: 457

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

<ul>
@foreach( var productData in products.Take(products.Count / 2))
{
    <li><span>@productData.Key</span> : <label>@productData.Value</label></li>
}
</ul>

<ul>
@foreach( var productData in products.Skip(products.Count / 2))
{
    <li><span>@productData.Key</span> : <label>@productData.Value</label></li>
}
</ul>

But consider to pass two dictionaries to your view instead.

Upvotes: 3

Related Questions