Use of ASP. NET MVC2 Ajax JSON

Source: Internet
Author: User

Learn to read 145 comments 01:55:59 0 font size: LargeMediumSmall subscription

JSON (JavaScript Object Notation) is a simple data format, which is lighter than XML. JSON is a javascript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.

JSON rules are simple: the object is an unordered "'name/value' pair" set. An object starts with "{" (left brace) and ends with "}" (right brace. Each "name" is followed by a ":" (colon); "Use commas (,) to separate 'name/value' pairs. Detailed reference http://www.json.org/json-zh.html

Example: A: [{"name": "zhangsan"}]

B: [{"name": "zhangsan", "sex": "Man"}]

C: [{"name": "zhangsan", "sex": "Man" },{ "name": "Lili", "sex": "woman"}]

Each expansion number in it is one item. for c json, we can use C [0]. name to retrieve the zhangsan field, C [1]. sex gets the sex "woman" corresponding to Lili and the second item"

In this way, we can easily upload the values of each field in the database to the page. Here I will use the MVC2 architecture. The databases used are as follows:

Here I will only demonstrate the usage in a simple way. As for the page effect, there will be no exquisite design. The page is very simple. Just click the button to display the powername and powercnname of the second item.

The view layer is as follows:

<Head runat = "server">

<Title> test </title>

<SCRIPT src = ".../scripts/jquery-1.4.1.js" type = "text/JavaScript"> </SCRIPT>

</Head>

<Body>

<Div>

Permission: <input type = "text" id = "name"/> <br/>

Chinese name: <input type = "text" id = "cname"/> <br/>

<Input type = "button" id = "show" value = "Show second"/>

</Div>

<SCRIPT type = "text/JavaScript">

$ (Document). Ready (function (){

$ ("# Show"). Click (function getdate (){

$. Ajax ({

URL: "getpower ",

Type: "Post ",

Datatype: "JSON ",

Success: getdataresult

});

});

Function getdataresult (data ){

$ ("# Name"). Val (data [1]. Name );

$ ("# Cname"). Val (data [1]. chinesename );

}

});

</SCRIPT>

</Body>

The model layer creates a class named power, specifically responsible for reading data.

Public class power

{

Crmdatacontext DB = new crmdatacontext ();

Public list <crm_power> getallpowers ()

{

Return dB. crm_power.tolist ();

}

}

The Controller layer mainly writes an action for Ajax to call JSON.

Public actionresult test ()

{

Return view ();

}

Public actionresult getpower ()

{

Power P = new power ();

VaR powers = P. getallpowers ();

Arraylist list = new arraylist ();

Foreach (VAR s in powers)

{

Hashtable HA = new hashtable ();

Ha. Add ("name", S. powername );

Ha. Add ("chinesename", S. powercnname );

List. Add (HA );

}

Return this. JSON (list );

}

The page effect is as follows. If you use firebug debugging, you can also find the corresponding page as follows:

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.