如何在MVC中拆分字符串并构建ul

Fil*_* DO 0 c# html5 html-lists asp.net-mvc-4

我需要拆分逗号分隔的字符串并构建一个<ul>.

我似乎找不到最佳点.

这是我的代码:

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

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

以下是上述代码的结果.

如何在MVC中构建列表项ul

Dan*_*mms 5

尝试@从中加入ps[i].

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

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

也没有什么理由使用普通for循环,foreach更具可读性,同样高效.

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