2012-08-27 248 views
0

我有一個字符串的集合中的重複MVC3剃刀用下面的代碼的格式採集:MVC3剃鬚刀 - 串

 @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剃刀一個簡單的方法來實現該W沒有必要將'if'和'foreach'語句結合起來?

回答

5
@string.Join(",",Model.Publications.Select(p=>"<text>"+ p.Title+ "</text>")) 
5
string.Join(", ", model.Publications.Select(pub => pub.Title).ToArray()) 
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> 
     } 
+0

這是選擇之一,但我自己像Podiluska ANS – Avi