Return Json to jQuery using 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 someone help me ..

0


source to share


2 answers


Just use the Json method of the controller to serialize the type and return the JsonResult:



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

      

+8


source


I enjoy working with the Newtonsoft json library. this allows you to control the json serialization process so you can tell what to do with null values, etc.

eg



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

      

+4


source







All Articles