Ich benutze Entity-Framework, um Daten aus der Datenbank zu erhalten und serialisieren sie zu JSON. Und ich möchte, dass meine JSON-Antwort wie folgt aussieht. Shoud ich hinzufügen items
Eigenschaft zu meinem Modell und machen JSON ich will? Vielen Dank.Wie komplexe Json-Objekt in asp.net mvc zurückgegeben werden
Desired Json
{
"items" : [
{
"Id": 1,
"AdContent":"Content1"
},
{
"Id": 2,
"AdContent":"Content2"
},
{
"Id": 3,
"AdContent":"Content3"
}
]
}
Aktuelle JSON Ich erhalte
[
{
"Id":1,
"AdContent":"Content1"
},
{
"Id":2,
"AdContent":"Content2"
},
{
"Id":3,
"AdContent":"Content3"
}
]
{
-Controller
public JsonResult GetJson()
{
using (var db = new DoskaUsContext())
{
List<AdViewModel> list = db.Ads.Select(x => new AdViewModel
{
Id = x.AdId,
AdContent = x.AdContent
}).ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
}
Modell
public class AdViewModel
{
public int Id { get; set; }
public string AdContent { get; set; }
}
'Rückkehr Json (neu {items = list}, JsonRequestBehavior.AllowGet);' –