asp.net Mvc+jquery+newtonsoft.json Happy Ajax

Source: Internet
Author: User
Tags datetime iis json serialization string format tostring

This is my plan at present, personally feel quite lightweight and comfortable. Controller is responsible for the sequence of data to be output into JSON.

Html.actionurl This method the original MVC Toolkit not, I conveniently Add.

I was using the Newtonsoft.json crop parts sequence into JSON, so why not use MS Ajax built in System.Web.Script.Serialization.JavaScriptSerializer, is because he has the datetime sequence in string format, the client side can not be directly taken. Newtonsoft.json part of me is also a little change, so he can do the serialization of value Type, can be referenced.

Incidentally, I was tested on IIS 5.1 and it would have been difficult to see the compatibility test for IIS 6.0 and ASP.net 3.5/vs 2008, presumably to test MVC Web Project more simply, install the. NET Framework 3.5 Reload asp.net 3.5 Extensions Preview (yes, currently only Preview version), directly on the OK.

The following process Controller (ajaxtest)-> view (ajaxpage)-> Controller (Ajax, id=1)-> view (ajaxpage) gets JSON->controller (Ajax, id=2)-> View (ajaxpage) Gets the JSON-> end, presumably. (If the picture will be more beautiful)

ControllerBase.cs
public class Controllerbase:controller {
public void Renderjson (object obj) {
String jsonstring = Newtonsoft.Json.JavaScriptConvert.SerializeObject (obj);
Response.Clear ();
response.contentencoding = Encoding.UTF8;
Response.ContentType = "Application/json";
Response.Write (jsonstring);
Response.Flush ();
Response.End ();
}
}
HomeController.cs
public class Homecontroller:controllerbase {
[Controlleraction]
public void Ajaxtest () {
Renderview ("Ajaxpage");
}
[Controlleraction]
public void Ajax (int id) {
Switch (ID) {
Case 1:
Renderjson (DateTime.Now.ToString ());
Break
Case 2:
order[] orders = new order[] {
New Order () {pk=1, title= "B001", OrderDate = DateTime.Now},
New Order () {pk=2, title= "A003", OrderDate = DateTime.Now}
};
Renderjson (orders);
Break
Case 3:
int n1,n2;
Int. TryParse (request["N1"],out N1);
Int. TryParse (request["n2"],out n2);
Renderjson (n1 + n2);
Break
}
}
}
}
Ajaxpage.aspx
<script language= "javascript" type= "Text/javascript" src= "http://jqueryjs.googlecode.com/files/" Jquery-1.2.1.min.js "></script>
<script language= "javascript" type= "Text/javascript" >
$ (document). Ready (function () {
var actionUrl1 = ' <%=html.actionurl ("Ajax", "/1")%> ';
var actionUrl2 = ' <%=html.actionurl ("Ajax", "/2")%> ';
var actionUrl3 = ' <%=html.actionurl ("Ajax", "/3")%> ';
$ ("#link1"). Click (function () {
$.getjson (ACTIONURL1, {}, Function (JSON) {
Alert ("Server time:" + JSON);
});
});
$ ("#link2"). Click (function () {
$.getjson (ActionUrl2, {}, Function (JSON) {
Alert ("Total" + json.length.toString () + "records");
for (Var i=0;i<json.length;i++) {
Alert (json[i). PK + "," + json[i]. Title + "," + json[i]. OrderDate);
}
});
});
$ ("#t1, #t2"). Change (function () {
$.getjson (ActionUrl3, {"N1": $ ("#t1"). Val (), "N2": $ ("#t2"). Val ()}, function (JSON) {
$ ("#t3"). Val (json.tostring ());
});
});
});
</script>
Ajax page<ol>
<li><a id= "Link1" href= "#" >get Server time</a></li>
<li><a id= "Link2" href= "#" >return object</a></li>
<li>
<input type= "text" name= "T1" id= "T1" maxlength= "4" style= "width:40px"/>
+
<input type= "text" name= "T2" id= "T2" maxlength= "4" style= "width:40px"/>
=
<input type= "text" name= "T3" id= "T3" maxlength= "4" readonly= "readonly" style= "width:40px"/>
</li>
</ol>

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.