【问题标题】:how to pass an integer array from a asp.net mvc controller action back to jquery function如何将整数数组从 asp.net mvc 控制器操作传递回 jquery 函数
【发布时间】:2010-04-04 14:45:42
【问题描述】:

这是我的 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 函数?

【问题讨论】:

    标签: jquery asp.net-mvc ajax arrays


    【解决方案1】:

    将其作为 JsonResult 而不是 ActionResult 返回,javascript 可以轻松处理。见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);
             });
    

    【讨论】:

    • @James Kolpack - 像这样?公共 ActionResult GetList(int id) { int[] list = new int[] {1, 2, 4};返回 JSON(列表); }
    • @James Kolpack - 我知道出了什么问题。你让我很接近,但你还需要在 this.json 中添加一件事)你必须再添加一个参数 JsonRequestBehavior.AllowGet);请更新这个,我会接受你的回答
    • JsonRequestBehavior.AllowGet 可能是 ASP.NET-MVC2 特定的,也许?
    • 是的,AllowGet 是 MVC2 的新功能。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多