1

I've seen a variety of ways people do it, but It's still failing to set the values for the arrays "comments" and "commentCriterions" in the controller. Any help would be much appreciated.

Edit: I managed to use JSON.stringify

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

The array is set, but set incorrectly

 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.")
            }
        },
    })
}

Controller

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

After messing with the function this is what the ajax call looks like, resulting in a 500 (Internal Server Error) after setting contentType in ajax.

    $.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.")
            }
        },
    })
7
  • 1
    Are you sure your javascript is getting the values you're expecting? Log out the comments and commentCriterions array before sending it across the wire and see if they have the data in them. Commented Feb 11, 2015 at 21:23
  • Remove the quotes around the argument names in the ajax call: i.e. data: { comments: comments, commentCriterions: commentCriterions, ...etc... } Commented Feb 11, 2015 at 21:34
  • To BFree - Yes, I used console.log() to test that the values were being set correctly. Commented Feb 11, 2015 at 21:49
  • 1
    You need to set contentType: "application/json; charset=utf-8", in the ajax options. Commented Feb 11, 2015 at 21:56
  • 1
    You also need to stringify the data. e.g. var data = { comments: comments, commentCriterions: commentCriterions, ..}; then data: JSON.stringify(data), Commented Feb 11, 2015 at 23:20

1 Answer 1

1

When posting arrays using traditional: true, you need to stringify your data and include 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.")
    }
  }
})

Side notes

  1. You can use the jQuery .map() function to easily generate your arrays, for example var comments = $(someSelector).map(function () { return $(this).val(); }).get();
  2. Consider returning null rather than return Json(false);. Then its just if(!success) { alert(..); }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.