Passing JavaScript array to IEnumerable gives null values

I'm having a huge amount of trouble passing a Javascript array to my controller in MVC 3. I get null values ​​all the time and feel like I've tried every way of passing an array. Below are the JavaScript corresponding to the view model for Questions

and the signature of the controller. Any help would be appreciated. I am not wrong about my JavaScript and I think I must be missing something fundamental.

The values ​​for id

and response-id

are accepted correctly on the controller.

Javascript

$("#form-submit-scores").submit(function () {

        var question = [],
        var item = [],

        $('.questionRow').each(function (index) {
            question[index] = new Array();
            var fullQuestionId = $(this).attr('id');
            var fullQuestionParts = fullQuestionId.split('-');
            question[index].QuestionId = fullQuestionParts[fullQuestionParts.length - 1];
            question[index].QuestionScore = $('.scoreBoard').val();
        });

        $('.itemRow').each(function (index) {
            item[index] = new Array();
            item[index].ItemId = $(this).attr('id');
            item[index].ItemScore = $('.scoreBoard').val();
        });

        var url = "/ctr/SaveResponse",
            data = {
                Id: $('#id').val(),
                ResponseId: $('#response-id').val(),
                Questions: question,
                Items : item
            },

        if (isSubmitScores) {
            url = "/ctr/SubmitResponse"
        }

        $.ajax({
            url: url,
            type: 'Post',
            data: data,
            traditional:true,
            datatype: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (!result.Success) {

....
....
....

      

ViewModels

public class SubmitResponseViewModel
    {
        public int Id { get; set; }
        public int ResponseId { get; set; }
        IEnumerable<SubmitResponseScoresQuestionViewModel> Questions {get;set;}
        IEnumerable<SubmitResponseScoresItemViewModel> Items { get; set; }
    }



public class SubmitResponseScoresQuestionViewModel
    {
        public int QuestionId { get; set; }
        public decimal? QuestionScore { get; set; }
    }

      

controller signature

public JsonResult SubmitResponseScores(SubmitResponseScoresViewModel model)

      

So, as I said above, my model now contains the correct values ​​for id

and response-id

, but null values ​​for Questions

and Items

. I have confirmed that mine is being data

populated in the AJAX call, so I think I am not providing data in the appropriate format for the controller.

EDIT: 1

Chrome JS Debugger: AJAX Data Object

  JSON.stringify(data, null, 2)
"{
  "Id": "1027",
  "ResponseId": "26",
  "Questions": [
    {
      "QuestionId": "7",
      "QuestionScore": "0"
    },
    {
      "QuestionId": "2",
      "QuestionScore": "0"
    },
    {
      "QuestionId": "1",
      "QuestionScore": "0"
    }
  ],
  "Items": [
    {
      "ItemId": "434",
      "ItemScore": "0"
    }
  ]
}"

      

+3


source to share


2 answers


You want to serialize your array to JSON using JSON.stringify
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

1) set Json.net to Nuget to enable JsonFilter notation

PM> Install-Package Newtonsoft.Json

      

2) put Json Filter annotation on your action method



[JsonFilter(Param = "attributes", JsonDataType = typeof(AttributeViewModel[]))]
  JsonResult SubmitResponseScores(SubmitResponseScoresViewModel model)

      

3) When calling ajax:

    data:  JSON.stringify(data), 

      

+2


source


Your collections for Questions and Items must be public, otherwise they will not be attached to the model on the back



0


source







All Articles