我想以json格式输出此循环
[{
@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 :(得分:4)
不要手动构建JSON。相反,构建一个对象图,然后对其进行JSON编码:
@{
var data = Model.urunler.Where(u => u.Id = item.uid).Select(u => new
{
id = item.id,
urunadi = urun.urunadi
});
}
@Html.Raw(Json.Encode(data))