Trouble Creating JSON with MVC3 (asp.net) using static Json class (system.web.helpers.json)
I'm having trouble creating the following JSON using uisng the C# MVC3 system.web.helpers.json namespace. Here is what I am trying to form:
{"success开发者_StackOverflow中文版":true,"msg":"", "Data": [ { "Id":167 } ] }
What I have tried is this (with no success)
var x = Json(
new {Id = result.SponsorListId});
return Json(new
{
success,
msg = success ? "" : "sponsorListResult Passed Into Update as null",
Data = new List<Json>() {x}
}, JsonRequestBehavior.DenyGet);
I've tried lots of other things also, but no point in listing all my failures.
Thanks for any help on this.
Basically all the Json() method does is serialize the object you send it. When you send it a List you are sending it a JsonResponse which isn't what you are intending. What you should do is:
return Json(new
{
success,
msg = success ? "" : "sponsorListResult Passed Into Update as null",
Data = new []{ new { Id: result.SponsorListId } }
}, JsonRequestBehavior.DenyGet);
Which should serialize out to where you want it to be.
精彩评论