Detailed Description: mvc uses JsonResult to return Json data, jsonresultjson
Controller defines the following methods:
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 =. artTitle, yy =. artPublishTime }). take (5); // remember to select new here; otherwise, an error is returned: serialization type System. data. entity. cyclic references are detected when DynamicProxies XXXXX is an object. // Add this statement without selecting new. // db. contextOptions. proxyCreationEnabled = false; res. data = list; // return list var name = "Xiaohua"; var age = "12"; var name1 = "Xiaohua"; var age1 = "12"; res. data = new object [] {new {name, age}, new {name1, age1 }}; // return a custom object array var person = new {Name = "James", Age = 22, Sex = "male"}; res. data = person; // returns a single object; res. data = "this is a string"; // returns a string, meaning little; res. jsonRequestBehavior = JsonRe QuestBehavior. AllowGet; // GET is allowed. Otherwise, an error is returned. 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") {//} metadata (o).html (data [0]. name); obj. attr ("actionValue", data [0]. result) ;}}}</script>
The above is used in mvc. How can we use it in webform?
Reference Newtonsoft. Json. dll in webform;
You can also splice strings by yourself.
Protected void Page_Load (object sender, EventArgs e) {var customer = new customer {name = "Li Hua", sex = "male "}; var customer1 = new customer {name = "xiaofang", 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 \": \ "xiaofang \", \ "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> <div id="dataDiv"> ggg </div> <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/>"; }); }); $("#dataDiv").html(tt); }); } </script>
Display result:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.