Aristotle Ucab
Aristotle Ucab

Reputation: 74

Return Json to jQuery with ASP.NET MVC

I have this code..

        Models.Person p = new testmvc.Models.Person { Firstname = "yongeks", Lastname = "ucab" };

        Models.Person p2 = new testmvc.Models.Person { Firstname = "lyn", Lastname = "torreon" };

        string q = JavaScriptConvert.SerializeObject(new String[] { JavaScriptConvert.SerializeObject(p), JavaScriptConvert.SerializeObject(p2) });

        Console.WriteLine(q);

        return q;

i need to parse this code into jquery.. using json request.. can somebody help me..

Upvotes: 0

Views: 2847

Answers (2)

redsquare
redsquare

Reputation: 78667

I like working with the Newtonsoft json library. it allows you greater control over the json serialization process so you can specify what to do with null values etc

e.g

  JsonNetResult jsonNetResult = new JsonNetResult();
  jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
  jsonNetResult.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  jsonNetResult.Data = nodes
  return jsonNetResult;    

Upvotes: 4

Mike Scott
Mike Scott

Reputation: 12463

Just use the controller's Json method to serialize the type and return a JsonResult:

Models.Person p2 = new testmvc.Models.Person { Firstname = "lyn", Lastname = "torreon" };
return Json( p2 );

Upvotes: 8

Related Questions