I am using entity framework to get data from database and serialize it to JSON. And I want my JSON response looks like the one below.
Shoud I add items
property to my model and make JSON I want? Thanks.
Desired Json
{
"items" : [
{
"Id": 1,
"AdContent":"Content1"
},
{
"Id": 2,
"AdContent":"Content2"
},
{
"Id": 3,
"AdContent":"Content3"
}
]
}
Current JSON I receive
[
{
"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);
}
}
Model
public class AdViewModel
{
public int Id { get; set; }
public string AdContent { get; set; }
}
return Json(new { items = list }, JsonRequestBehavior.AllowGet);
– Stephen Muecke Aug 23 at 3:46