传递一个JavaScript数组的IEnumerable给空值 [英] Passing a JavaScript array to IEnumerable gives null values

查看:140
本文介绍了传递一个JavaScript数组的IEnumerable给空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的传递JavaScript数组我控制器MVC 3,我不断收到空值,感觉就像我试图传递数组每路的麻烦量。下面是JavaScript,相关视图模型为问题和控制器签名。我倒是AP preciate任何帮助。我没有收到我的JavaScript的任何错误,我想我一定是失去了一些东西根本。

ID 值和响应-ID 正在在控制器正确接收。

的JavaScript

  $(#形式提交分数)。递交(函数(){        VAR问题= []
        VAR项= []        $('。questionRow')。每个(函数(指数){
            问题[指数] =新的Array();
            VAR fullQuestionId = $(本).attr('身份证');
            变种fullQuestionParts = fullQuestionId.split(' - ');
            问题[指数] .QuestionId = fullQuestionParts [fullQuestionParts.length - 1];
            问题[指数] .QuestionScore = $('记分牌。)VAL()。
        });        $('。itemRow')。每个(函数(指数){
            项目[指数] =新的Array();
            项目[指数] .ItemId = $(本).attr('身份证');
            项目[指数] .ItemScore = $('记分牌。)VAL()。
        });        VAR URL =/ CTR / SaveResponse
            数据= {
                ID:$('#ID)VAL()。
                ResponseId:$('#响应-ID)VAL()。
                问题:问题,
                项目:项目
            },        如果(isSubmitScores){
            URL =/ CTR / SubmitResponse
        }        $阿贾克斯({
            网址:网址,
            输入:邮报,
            数据:数据,
            传统:真实,
            数据类型:JSON
            的contentType:应用/ JSON的;字符集= UTF-8,
            成功:函数(结果){
                如果(!result.Success){....
....
....

的ViewModels

 公共类SubmitResponseViewModel
    {
        公众诠释标识{搞定;组; }
        公众诠释ResponseId {搞定;组; }
        IEnumerable的< SubmitResponseScoresQuestionViewModel>问题{获取;设置;}
        IEnumerable的< SubmitResponseScoresItemViewModel>项目{搞定;组; }
    }公共类SubmitResponseScoresQuestionViewModel
    {
        公众诠释QuestionId {搞定;组; }
        公共小数? QuestionScore {搞定;组; }
    }

控制器签名

 公共JsonResult SubmitResponseScores(SubmitResponseScoresViewModel模型)

因此​​,正如我上面所说的,我的模型现在包含编号响应-ID ,但空的正确的价值观为问题值产品。我已经证实了我的数据被填充在AJAX调用,所以我在想,我不是在控制器的适当的格式提供数据。

编辑:1

铬JS调试器:AJAX数据对象。

  JSON.stringify(数据,空,2)
{
  ID:1027
  ResponseId:26,
  问题:
    {
      QuestionId:7,
      QuestionScore:0
    },
    {
      QuestionId:2,
      QuestionScore:0
    },
    {
      QuestionId:1,
      QuestionScore:0
    }
  ]
  项目:
    {
      ITEMID:434
      ItemScore:0
    }
  ]
}


解决方案

您想序列透过JSON.stringify结果阵列以JSON
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

1)的NuGet安装Json.net为包括JsonFilter符号

  PM>安装封装Newtonsoft.Json

2)把一个JSON筛选注释你的操作方法

  [JsonFilter(参数=属性,JsonDataType = typeof运算(AttributeViewModel []))]
  JsonResult SubmitResponseScores(SubmitResponseScoresViewModel模型)

3)在你的Ajax调用:

 数据:JSON.stringify(数据)

I'm having a huge amount of trouble passing a Javascript array to my controller in MVC 3. I keep getting null values and feel like I have tried every way of passing the array. Below is the JavaScript, the relevant view models for the Questions and the controller signature. I'd appreciate any help. I'm not getting any errors in my JavaScript and I think i must be missing something fundamental.

The values for id and response-id are being received correctly at 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 my data is being populated in the AJAX call so i'm thinking that I'm not providing the 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"
    }
  ]
}"

解决方案

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) install Json.net in Nuget to include the JsonFilter notation

PM> Install-Package Newtonsoft.Json

2) place a Json Filter annotation on your action method

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

3) In your ajax call:

    data:  JSON.stringify(data), 

这篇关于传递一个JavaScript数组的IEnumerable给空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆