代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

如何将整数数组从ASP.NET MVC控制器操作传递回jquery函数

  •  1
  • leora Matt Lacey  · 技术社区  · 14 年前

    这是我的jquery代码:

     $.get('/Home/GetList', function(data) {
                 debugger;
                 $('#myMultiSelect').val(data);
             });
    

    这是我的控制器代码:

        public ActionResult GetList(int id)
        {
            int[] bodyParts = _repository.GetList(id);
    
           //how do i return this as an array back to javascript ??
        }
    

    如果我有 GetList 函数返回一个整数数组,如何将其返回到jquery函数?

    1 回复  |  直到 14 年前
        1
  •  3
  •   James Kolpack    14 年前

    将其作为jsonresult而不是actionresult返回,这是Javascript可以轻松处理的。见A blog article here .

    这看起来像:

    public JsonResult GetList(int id)
    {
       int[] bodyParts = _repository.GetList(id);
    
       return this.Json(bodyParts);
    }
    

    然后使用 getJSON() 检索它:

     $.getJSON('/Home/GetList', null, function(data) {
                 debugger;
                 $('#myMultiSelect').val(data);
             });