0

我有一组在 MVC3 Razor 中重复的字符串,代码如下:

        @if (Model.Publications != null)
        {
            <tr>
                <th>Publications</th>
                <td>
                    @foreach (var publication in @Model.Publications)
                    {
                        <text>@publication.Title</text>
                    }
                </td>
            </tr>
        }

现在,当我显示这个时,我得到的只是:

Book1Book2Book3

但我真正想要的是这样的:

Book1, Book2, Book3

MVC razor 中是否有一种简单的方法来实现这一点,而无需结合“if”和“foreach”语句?

4

3 回答 3

5
string.Join(", ", model.Publications.Select(pub => pub.Title).ToArray())
于 2012-08-27T13:57:27.120 回答
5
@string.Join(",",Model.Publications.Select(p=>"<text>"+ p.Title+ "</text>"))
于 2012-08-27T13:57:35.683 回答
1
@if (Model.Publications != null)
        {
            <tr>
                <th>Publications</th>
                <td>
                    @var first = true
                    @foreach (var publication in @Model.Publications)
                    {
                        <text>@string.format("{0}{1}", first ? "" : ", ",  publication.Title)</text> 
                        @first = false;
                    }
                </td>
            </tr>
        }
于 2012-08-27T13:58:40.413 回答