How to use Jsonresult

Source: Internet
Author: User
This article mainly introduces the details of MVC using Jsonresult return JSON data, with a certain reference value, interested in small partners can refer to.

The following methods are defined in the controller:

Public Jsonresult updatesingle (int id, string actionname, String actionvalue)   {    var res = new Jsonresult ();    var value = "Actionvalue";    Db. contextoptions.proxycreationenabled = false;    var list = (from a in db. Articles       Select New       {        name = A.arttitle,        yy = A.artpublishtime       }). Take (5);    Remember here to select New otherwise error: A circular reference was detected when serializing an object of type System.Data.Entity.DynamicProxies XXXXX.    //Do not select New also line add this sentence//db. contextoptions.proxycreationenabled = false;    Res. Data = list;//returns     the list var name = "Xiao Hua";    var age = "n";    var name1 = "Xiao Hua";    var age1 = "n";    Res.  Data = new object[] {new {name, age}, new {name1, age1}};//Returns a custom object array var person     = new {name = "xiaoming", age =, Sex = "Male"};    Res. Data = person;//Returns a single object;     Res. Data = "This is a string";//returns a string with little meaning;     Res. Jsonrequestbehavior = jsonrequestbehavior.allowget;//is allowed to be obtained using GET, otherwise error is obtained with Get.    return res;   }

Page Call:

<a href= "javascript:void (0);" onclick= "Javascript:upclick (this);" >click me</a> <script type= "text/javascript" >  function UpClick (o) {   var obj = $ (o);   alert (obj);   $.ajax ({      URL: "/articles/updatesingle?ran=" + math.random (),    type: "GET",    dataType: "JSON",    data: { ID:OBJ.ATTR ("id"), actionName:obj.attr ("ActionName"), ActionValue:obj.attr ("Actionvalue")},    success:function (data) {//    if (Data.result = = "True") {//     alert ("Modified successfully!") ");    }//    if (obj.attr ("actionname") = = "Artverify") {  //    }     $ (o). html (data[0].name);     Obj.attr ("Actionvalue", Data[0].result);    }   )  } </script>

The above is used in MVC, how to use it in WebForm?

In the WebForm to refer to Newtonsoft.Json.dll;

Of course you can also stitch your own strings.

protected void Page_Load (object sender, EventArgs e)   {    var customer = new Customer {name = "Li Hua", sex = "male"};
  
   var customer1 = new Customer {name = "Small Fang", sex = "female"};    var li = new list<customer> ();    Li. ADD (customer);    Li. ADD (customer1);    var list = Newtonsoft.Json.JavaScriptConvert.SerializeObject (LI);    var tt = "[{\" name\ ": \" Li hua \ ", \" sex\ ": \" male \ "},{\" name\ ": \" Small fang \ ", \" sex\ ": \" female \ "}]";    New Newtonsoft.Json.JsonSerializer (): (customer);    Response.Write (TT);    Response.End ();   }    public class customer   {public    string name {get; set;}    public string Sex {get; set;}   }
  

Page Method:

<p>   <a href= "javascript:void (0)" onclick= "Javascript:getjsondata ();" >GetJsonData</a>  </p>  <p id= "Datap" >   ggg  </p>  <script type= " Text/javascript ">   function Getjsondata () {    var str =" ";    $.getjson ("/json.aspx", function (data) {     var tt = "";     $.each (data, function (k, v) {      $.each (V, function (KK, VV) {       tt + = KK + ":" + vv + "<br/>";      });     });     $ ("#datap"). HTML (TT);    });   }  </script>

Show Results:

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.