ASP. net mvc 2.0 interacts with JSON data in jquery

Source: Internet
Author: User

The Asynchronous interaction between the client and the server is implemented using the Ajax method provided by jquery. The following describes how to use Ajax functions in the extracted jquery API:

There is a data parameter that allows you to transmit data to the server. jquery will serialize the parameter data based on the post or get protocol.

If the submitted data uses complex JSON data, for example:

{Userid: 1, userinfo: {name: "Lily", age: "21 "}}

The server cannot normally receive complete parameters because jquery serializes data and uses key-value pairs to assemble the data. Parameters are assembled into userid = 1 & userinfo = object; the objects pointed to by userinfo are serialized into strings "object ".

How can I submit a complex object to the background action parameter? Here we provide a JSON. js file whose serialized data is in JSON format (see the source code). The $. tojson (PARAM) function is called, and you can also write it yourself.

 

var json = { Name: "Sherry", Age: "21" };

$("#btnJson_post").click(function () {
$.ajax({
type: "POST",
url: "/Home/AcceptJson",
dataType: 'json',
data: $.toJSON(json),
contentType: 'application/json; charset=utf-8',
success: function (msg) { alert(msg.Name); }
});
});

Server:

Person. CS

[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

Homecontroller

[HttpPost]
public JsonResult AcceptJson(Person person)
{
return Json(new Person { Name=person.Name,Age=person.Age });
}

 

However, the person parameter on the server cannot receive data. Because deserialization of data cannot be performed in JSON format, you need to add references to Microsoft in MVC 2.0. web. MVC. DLL, and in the global. asax. add the following code at the end of the CS file in application_start:

Valueproviderfactories. Factories. Add (New jsonvalueproviderfactory ());

In this way, MVC can deserialize data in JSON format and process the data and return results. Source code download

Also can refer to: http://www.mikel.cn/%e5%bc%80%e5%8f%91%e7%ac%94%e8% AE %b0/%e8%bd%ac%e8%bd%bdasp-net-mvc%e4%b8%ad%e7%9a%84json-binding%e5%92%8cvalidate.html

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.