2015-02-11 125 views
0

我已经看到了人们做这件事的各种方式,但它仍然无法为数组“comments”和“commentCriterions”设置值控制器。任何帮助将非常感激。如何使用Ajax(jquery)将数组发送到控制器(MVC)

编辑:我设法利用JSON.stringify

data: { 
      'comments': JSON.stringify(comments), 

阵列被设置,但不正确

comments[0] = "[\"zxczxczx\",\"Another boring comment\",\"Why is this broken?!\",\"GASP!\"]" 

设置的JQuery

function saveComment(popupId) { 
    var textArea = popupId.find('@commentClass'); 
    var comments = []; 
    var commentCriterions = []; 
    for (var i = 0; i < textArea.length; i++) { 
     comments[i] = textArea.val(); 
     commentCriterions[i] = textArea.attr("data-criterionid"); 
    } 

    $.ajax({ 
     url: "SaveComment", 
     method: "post", 
     dataType: 'json', 
     traditonal: true, 
     data: { 
      'comments': comments, 
      'commentCriterions': commentCriterions, 
      'observationId': observationId, 
      'reviewingId': '@reviewingId' 
     }, 
     success: function (status) { 
      if (status == "False") { 
       alert("The ID number of the person currently being reviewed has changed, please refresh the page and try again. Any changes to this observation will not be saved.") 
      } 
     }, 
    }) 
} 

控制器

public bool SaveComment(string[] comments, string[] commentCriterions, string observationId, string reviewingId) 
    { 
     int breakPoint = 0; 
     return true; 
    } 

在调用函数之后,这就是ajax调用的样子,在ajax中设置contentType后导致500(内部服务器错误)。

$.ajax({ 
     type: "POST", 
     url: "SaveComment", 
     dataType: "json", 
     data: { 
      'comments': JSON.stringify(comments), 
      'commentCriterions': JSON.stringify(commentCriterions), 
      'observationId': JSON.stringify(observationId), 
      'reviewingId': JSON.stringify('986509040'), 
     }, 
     contentType: 'application/json; charset=utf-8', 
     traditonal: true, 
     success: function (status) { 
      if (status == "False") { 
       alert("The ID number of the person currently being reviewed has changed, please refresh the page and try again. Any changes to this observation will not be saved.") 
      } 
     }, 
    }) 
+1

你确定你的JavaScript得到你期望的值吗?在通过线路发送它们之前注销'comments'和'commentCriterions'数组,然后查看它们是否包含数据。 – BFree 2015-02-11 21:23:32

+0

除去ajax调用中参数名称周围的引号:ie data:{comments:comments,commentCriterions:commentCriterions,... etc ...} – itsme86 2015-02-11 21:34:46

+0

对BFree - 是的,我用console.log()来测试值正在被设置正确。 – 2015-02-11 21:49:19

回答

1

使用traditional: true,你需要字符串化数据并发布当阵列包括contentType: "application/json; charset=utf-8",

var data = { comments: comments, commentCriterions: commentCriterions, observationId: observationId, reviewingId: @reviewingId }; 
$.ajax({ 
    url: '@Url.Action("SaveComment")'; // always use @Url.Action! 
    method: "post", 
    dataType: 'json', 
    traditonal: true, 
    contentType: "application/json; charset=utf-8", // add this 
    data: JSON.stringify(data), // change this 
    success: function (status) { 
    if (status == "False") { 
     alert("The ID number of the person currently being reviewed has changed, please refresh the page and try again. Any changes to this observation will not be saved.") 
    } 
    } 
}) 

旁注

  1. 您可以使用jQuery的.map()功能可轻松地生成您 阵列,例如var comments = $(someSelector).map(function() { return $(this).val(); }).get();
  2. 考虑返回null而不是return Json(false);。然后 它只是if(!success) { alert(..); }
相关问题