将数组作为参数从JavaScript中的ajax调用传递给控制器

时间:2016-05-19 13:51:33

标签: javascript jquery asp.net ajax asp.net-mvc-4

我正在开发一个ASP.NET MVC 4网站,我遇到了一些功能问题。我解释一下,我要选择一个表中显示的实体及其链接的复选框:

Screenshot of my table where each row has a checkbox with the same Id as the entity

Console showing updates in the array

在我的脚本中,我已经能够将每个已检查的Id的复选框存储在一个数组中,如果取消选中该复选框,则将其删除。但是我无法将数组传递给我的控制器函数来删除数据库中的每个选定实体。 我使用来自jquery的$.ajax()来通过POST请求发送数组(作为JSON),但我总是得到500错误:

  • JSON原语无效
  • 空参考

这是我的脚本中的函数(我不知道我的数组的格式是否有效):

    var sendDocsToDelete = function (docsArray) {
        $.ajax({
            type: 'POST',
            url: 'Main/DeleteDocuments',
            data: JSON.stringify(docsArray),
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (result) {
                alert('Success ' + result.d);
            },
            error: function (result) {
                alert('Fail ' + result.d);
            }
        });
    }

然后,POST在我的控制器中调用以下函数:

    [Authorize]
    [WebMethod]
    public void DeleteDocuments(string docsToDelete)
    {
        int id; 
        string[] arrayDocs = JsonConvert.DeserializeObject<string[]>(docsToDelete);

        foreach (string docId in arrayDocs)
        {
            int.TryParse(docId, out id);
            dal.DeleteDocument(id); // dal = DataAccessLayer is the class which interacts with the database by executing queries (select, delete, update...)
        }
    }

更新2

    [Authorize]
    public ActionResult DeleteDocuments(int[] docsToDelete)
    {
        try{
               foreach (string docId in arrayDocs)
               {
                       int.TryParse(docId, out id);
                       dal.DeleteDocument(id); // dal = DataAccessLayer is the class which interacts with the database by executing queries (select, delete, update...)
               }
               return Json("Success");
        } 
        catch 
        {
               return Json("Error");
        }
    }


    var sendDocsToDelete = function (docsArray) {
        $.ajax({
            type: 'POST',
            url: 'Main/DeleteDocuments',
            data: docsArray,
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (result) {
                alert('Success ' + result.d);
            },
            error: function (result) {
                alert('Fail ' + result.d);
            }
        });
    }

有关此问题的任何想法?我希望我足够清楚。如果您需要更多细节,请不要犹豫。

4 个答案:

答案 0 :(得分:0)

也许JSON数组中的数据类型不是字符串? (如果你有[45,64,34,6]形式的数组,或[345,"wef4"]之类的混合数组,可能会发生这种情况。

要确保某些内容是Javascript中的字符串,您可以执行以下操作:var string = "".concat(otherVar);

答案 1 :(得分:0)

尝试将ajax数据更改为类似的内容..

 data : JSON.stringify({'docsToDelete':docsArray}),

答案 2 :(得分:0)

如果从Player正确传递整数数组(即您的$.ajax应该具有docsArray的值),那么您应该尝试:

[15,18,25,30,42,49]

更新:

您的javascript代码应为:

[Authorize]
public ActionResult DeleteDocuments(int[] docsArray)
{
    //int id; 
    //string[] arrayDocs = JsonConvert.DeserializeObject<string[]>(docsToDelete);
  try {
    foreach (int docId in docsArray)
    {
        //int.TryParse(docId, out id);
        dal.DeleteDocument(docId); // dal = DataAccessLayer is the class which interacts with the database by executing queries (select, delete, update...)
    }
    return "Success ";
  }
  catch {
        return "Error";
  }
}

答案 3 :(得分:0)

对您的代码进行这些更改。

在Jquery

data: docsArray,无需对数组进行字符串化

在控制器中

    [Authorize]                                                //remove [WebMethod]
    public ActionResult DeleteDocuments(string[] docsToDelete) //Add ActionResult, Change parameter to accept array
    {
        int id; 
        string[] arrayDocs = docsToDelete;     //no need of deserilization

        foreach (string docId in arrayDocs)
        {
            int.TryParse(docId, out id);
            dal.DeleteDocument(id); // dal = DataAccessLayer is the class which interacts with the database by executing queries (select, delete, update...)
        }

       return Json(id);    //return id back to ajax call...
    }