asp.net MVC3 JSON data Delivery usage instructions

Source: Internet
Author: User

asp.net MVC 3 has built-in binding support for JSON, making it easy to receive JSON-formatted data that is passed from the client. This article or the Android Blog project in the message small function to simply explain the specific use of the method. First look at the HTML code under the Razor view engine, which is primarily used to display a list of data for a message:

The code is as follows Copy Code
<div>
<script id= "usertemplate" type= "text/html" >
<div><a href= "${website}" >${UserName}</a> Says: ${content}</div>
</script>
<div id= "Contact" ></div>
<input type= "button" id= "Create" value= "create"/>
</div>

When we click the Create button to submit a message (here for simplicity, message content is fixed), execute jQuery Ajax to pass the JSON format data, as follows:

The code is as follows Copy Code

$ ("#create"). Click (function () {
var contact = {
UserName: "I love JQuery",
Website: "Http://www.google.com",
Content: "Nice to meet you."
};

$.ajax ({
URL: "/contact",
Type: "POST",
Data:contact,
DataType: "JSON",
Success:function (req) {
$ ("#userTemplate"). Render (req). Appendto ("#Contact");
}
});
});

If you are a pure asp.net developer and have no access to jquery, then I would strongly suggest that you learn jquery quickly, and jquery plays a very important role in ASP.net MVC 3, which is becoming more and more popular. Now you can refer to the great summary of jquery learning under (v) jquery Ajax. In this way, the message content is submitted to the background in the JSON structure via POST, and the ASP.net MVC 3 Controller receives the following code:

The code is as follows Copy Code
[httppost]
Public ActionResult Index (contacts Contact)
{
    if (modelstate.isvalid)
    {
        Android. Contact.addobject (contact);
        Android. SaveChanges ();
   }
    var contacts = from C in Android. Contact
                     where c.isvalid = 1
                     Select C;
    return Json (Contacts);
}

There is no new knowledge here, if you haven't seen the add, modify, delete operations in the front asp.net MVC 3, you can refer to the ASP.net MVC3 instance (vi) Add, modify, and delete operations (i), receive JSON visible, the Index () method, receive strongly typed Conta CT object as a parameter, asp.net MVC 3 can automatically bind JSON-formatted data to the. NET contact type on the server side, and we've covered this automatic binding in the ASP.net MVC 3 add operation. Visible, the assignment of the Entity data in ASP.net MVC 3 became very simple, and we had a save operation after we had the contact object, and here I think you've learned the convenience of receiving JSON in asp.net mvc 3. The program finally uses the JSON () method, which returns an object of the JSON structure, at Google


If you are not familiar with the use of Google Chrome debugging JS, you can refer to Google Chrome debugging JS Introduction, of course, you can also use FireFox Firebug to debug. The last jquery program uses the Microsoft jquery Templates plugin to display the JSON-formatted message data, making it unnecessary for us to iterate over the returned results. For jquery templates you can refer to the use of the Microsoft jquery Templates plug-in, the jquery plugin-Microsoft jquery templates. The final results are as follows:


In MVC 3, when you return a JSON type, the format of the time is "/date (1306418993027)/", so how do we transform the time format of the JSON return result? If you return the data in the JSON structure in Controller:

The code is as follows Copy Code

[HttpPost]
Public ActionResult Index (zcomment model)
{
return Json (model, jsonrequestbehavior.allowget);
}


In most cases we will not use this form of time format, we have written some code to deal with this problem, and share with you:

The code is as follows Copy Code

function Changedateformat (d)
{
var date = new Date (parseint (D.replace ("/date", ""). Replace (")/", ""), 10);
var month = PadLeft (Date.getmonth () + 1,10);
var currentdate = PadLeft (Date.getdate (), 10);
var hour=padleft (Date.gethours (), 10);
var minute=padleft (Date.getminutes (), 10);
return date.getfullyear () + "-" + month + "-" + currentdate+ "" +hour+ ":" +minute;
}

function PadLeft (str,min) {
if (str>=min)
return str;
Else
Return "0" +STR;
}

Call the method and find the result is what we need


As we have seen so far, it becomes very smart to receive JSON data in ASP.net MVC 3, while using the JQuery Templates plug-in makes binding JSON data very simple

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.