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层

 

相关文章:

  • 2021-12-31
  • 2021-08-27
  • 2022-01-14
  • 2021-10-05
  • 2021-08-10
  • 2021-12-29
  • 2021-09-24
  • 2021-08-18
猜你喜欢
  • 2022-02-21
  • 2021-09-26
  • 2021-12-09
  • 2021-09-01
  • 2021-04-24
相关资源
相似解决方案