How to split a string in MVC and build a ul

I need to split a comma separated string and build a <ul>.

I cannot seem to find the sweet spot.

Here is my code:

  <ul>
            @{
                string[] ps = Model.ProductsServices.Split(',');

                for (byte i = 0; i < ps.Length; i++)
                {
                    <li>ps[i]</li>
                }
            }
        </ul>

Here is the result of the above code.

How to build list items ul  in MVC

Upvotes: 0

Views: 5404

Answers (1)

Daniel Imms
Daniel Imms

Reputation: 50149

Try putting an @ in from of ps[i].

<ul>
    @{
        string[] ps = Model.ProductsServices.Split(',');

        for (byte i = 0; i < ps.Length; i++)
        {
            <li>@ps[i]</li>
        }
    }
</ul>

Also there is little reason to use a plain for loop, a foreach is more readable and just as performant.

var ps = Model.ProductsServices.Split(',');
foreach (string service in ps)
{
    <li>@service</li>
}

Upvotes: 5

Related Questions