2017-04-17 134 views
-6

我想輸出JSON格式的這個循環C#asp.net刪除最後一個逗號

[{ 



@foreach (var urun in Model.urunler.Where(u => u.Id == item.uid).ToList()) 
     { 
      <text>,"id":"@item.id","urunadi":"@urun.urunadi"</text> 
     } 



},] delete this last comma 

我想輸出: [{},{},{}]刪除此最後一個字符

+0

並非really clear你are asking what ....... –

+0

我want到delete此last comma。 – dfred

+0

請提供輸入,什麼是「這個」? – Berkay

回答

4

不要手動構建JSON。 Instead,build an object graph,和then JSON-encode it:

@{ 
    var data = Model.urunler.Where(u => u.Id = item.uid).Select(u => new 
    { 
     id = item.id, 
     urunadi = urun.urunadi 
    }); 
} 
@Html.Raw(Json.Encode(data)) 
+0

裏面有3個foreach循環。 – dfred

+1

沒關係。將整個事物構建爲對象圖,然後使用JSON編碼整個事物。嘗試在Razor中手動編寫JSON是非常荒謬的。 –