获取通过Ajax传递的控制器中的JSon.Stringify值

时间:2014-07-15 13:09:53

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

我有javascript函数:

 function savePreObservationQuestionAnswers()
    {
    var lvPreObservationQuestionAnswers = $("#lvPreObservationQuestionAnswers").data("kendoListView");
            var data = lvPreObservationQuestionAnswers.dataSource.data();
var preObservationAnswers = new Array();
        var totalNumber = data.length;
       var ArtifactsModel = [];
    for (var i = 0; i < totalNumber; i++) {
                var currentDataItem = data[i];
                var myjson = [];
                var preObservationAnswers = {};
                preObservationAnswers["EH_PP_OBSRVNFRM_QSTN_ID"] = currentDataItem.EH_PP_OBSRVNFRM_QSTN_ID;
                preObservationAnswers["EH_PP_OBSRVNFRM_QSTN_ANSWR_TXT"] = currentDataItem.EH_PP_OBSRVNFRM_QSTN_ANSWR_TXT;

                myjson.push(preObservationAnswers);

                ArtifactsModel.push(myjson);

                //preObservationAnswers.push(currentDataItem.EH_PP_OBSRVNFRM_QSTN_ID, $('#txtanswer_' + currentDataItem.EH_PP_OBSRVNFRM_QSTN_ID).val());

                //lstArtifactsModel.push(ArtifactsModel);
            }
            alert(ArtifactsModel);
            $.ajax({

                type: "post",
                url: "/PP/savePreObservationAnswers",
                data: {
                    objAllData: JSON.stringify(ArtifactsModel)
                },
                cache: false,
                success: function (data) {

                }
            });
    }

在控制器中,我的代码如下:

public JsonResult savePreObservationAnswers(IEnumerable<ArtifactsModel> objAllData)
{
 return Json("");
}

但我没有在控制器中获取objAllData的值。

我确信在javascript函数中我得到了正确的值。

我该怎么做?

修改

在ArtifactModel文件中,我有以下属性:

public int EH_PP_OBSRVNFRM_QSTN_ANSWR_ID { get; set; }
        public string EH_PP_OBSRVNFRM_QSTN_ANSWR_TXT { get; set; }
        public int EH_PP_ObservationID { get; set; }

        public int EH_PP_OBSRVFRM_QSTIN_SectionID { get; set; }

        public int EH_PP_OBSRVNFRM_QSTN_ID { get; set; }
        public string EH_PP_OBSRVFRM_QSTIN_SectionTitle { get; set; }
        public string EH_PP_OBSRVNFRM_QSTN_TXT { get; set; }

        public int EH_PP_OBSRVNFRM_QSTN_DSPSEQ { get; set; }

1 个答案:

答案 0 :(得分:1)

您必须将内容类型指定为JSON并对整个对象进行字符串化。目前你正在使用&#34;混合模式&#34;文字对象/ json无效:

data: {objAllData: JSON.stringify(ArtifactsModel) }

应该是:

data: JSON.stringify({ objAllData: ArtifactsModel })

最后是:

$.ajax({
    type: "post",
    contentType: 'application/json; charset=utf-8',
    url: "/PP/savePreObservationAnswers",
    data: JSON.stringify({ objAllData: ArtifactsModel }),
    cache: false,
    success: function (data) {
    }
});