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