将JavaScript数组传递给IEnumerable会给出null值

时间:2013-02-04 17:22:59

标签: c# javascript jquery asp.net-mvc asp.net-mvc-4

我在将一个Javascript数组传递给我在MVC 3中的控制器时遇到了很大的麻烦。我一直在获取空值,并且觉得我已经尝试过各种传递数组的方法。下面是JavaScript,Questions的相关视图模型和控制器签名。我很感激任何帮助。我的JavaScript中没有任何错误,我认为我必须遗漏一些基本的东西。

正在控制器上正确接收idresponse-id的值。

的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; }
    }

控制器签名

public JsonResult SubmitResponseScores(SubmitResponseScoresViewModel model)

正如我上面所说,我的模型现在包含Idresponse-id的正确值,但QuestionsItems的值为空。我已经确认我的data正在AJAX调用中填充,所以我认为我没有以适当的格式为控制器提供数据。

修改:1

Chrome JS调试器:AJAX数据对象

  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"
    }
  ]
}"

2 个答案:

答案 0 :(得分:3)

您希望使用JSON.stringify将数组序列化为JSON https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

1)在Nuget中安装Json.net以包含JsonFilter表示法

PM> Install-Package Newtonsoft.Json

2)在你的动作方法上放置一个Json Filter注释

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

3)在你的ajax电话中:

    data:  JSON.stringify(data), 

答案 1 :(得分:0)

您的问题和项目的集合需要公开,否则他们将不会在回发后绑定到模型