View層-->View層的JS function-->Controller-->返回JSON資料-->View層的JS function
Controller:
public ActionResult JsonHashTable()
{
ArrayList list = new ArrayList();
Hashtable ht1 = new Hashtable();
Hashtable ht2 = new Hashtable();
ht1.Add("Key", "value11");
ht2.Add("Key", "value222");
list.Add(ht1);
list.Add(ht2);
return Json(list, JsonRequestBehavior.AllowGet);
}
public ActionResult JsonModel()
{
ChangePasswordModel model = new ChangePasswordModel();
model.NewPassword = "gxw";
return Json(model, JsonRequestBehavior.AllowGet);
}
View層:
<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()" />
當我們進入index.aspx頁面後,出現兩個button按鈕,點擊HashTable就調用getHashTable函數,這個函數訪問Controller層JsonHashTable(),獲得JSON,然後返回給前台View層