ASP. NET MVC2 JSON

Source: Internet
Author: User

View layer --> View layer JS function --> Controller --> return JSON data --> View layer JS Function

 

Controller:

Public Actionresult jsonhashtable ()
{

Arraylist list =NewArraylist ();
Hashtable HT1 =NewHashtable ();
Hashtable ht2 =NewHashtable ();

ht1.add ( " key " , " value11 " );
ht2.add ( " key " , " value222 " );
list. add (HT1);
list. add (ht2);

return JSON (list, jsonrequestbehavior. allowget);
}

PublicActionresult jsonmodel ()
{
Changepasswordmodel model =NewChangepasswordmodel ();
Model. newpassword ="Gxw";
ReturnJSON (model, jsonrequestbehavior. allowget );
}

View layer:

 

< Script Type = "Text/JavaScript" >
Function Gethashtable (){
$. Getjson (
" /Home/jsonhashtable " , Function (DA ){
Alert (DA. Length );
});
}
Function GetModel (){
$. Getjson (
" /Home/jsonmodel " , Function (DA ){
Alert (DA. newpassword );
});
}
</ Script >

< Input Type = "Button" ID = "BTN" Value = "Hashtable" Onclick = "Gethashtable ()"   />
< Input Type = "Button" ID = "Button1" Value = "Model" Onclick = "GetModel ()"   />

 

When we enter the index. ASPX page, two buttons are displayed. Click hashtable to call the gethashtable function. This function accesses the Controller layer.Jsonhashtable (), get JSON, and then return it to the front view layer

 

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.