传递过数据的Ajax在C#中显示为null?

时间:2013-12-11 21:21:49

标签: c# javascript ajax asp.net-mvc json

我正在尝试将来自我的ajax帖子的JSON形式的一些数据发送到C#中的控制器方法。虽然我没有得到任何数据。我正在验证数据是否存在,因此在ajax的第二位之前有一个console.log(data.model)来做这件事。无论模型在C#方面总是为空。

Javascript Post:

$.ajax({
    data: model,
    cache: false,
    type: 'post',
    traditional: true,
    url: mod.RespondentsTableUrl,
    success: function (data) {
        console.log(data.model);
        if (data.success) {
            $('table.coldListTable').dataTable().fnRefreshTable(data.model);

            $('#numberOfResults').text(data.model.length);
            if (thereAreFiltersInPlace(model)) {
                $('#coldListFilterForm div.alert-container > div.nNote > p').text(String.format(Culture.Phrases.FiltersNoteFormat, data.model.length));
                $('#coldListFilterForm div.alert-container > div.nNote.dn').removeClass('dn');
            }
            else {
                $('#coldListFilterForm div.alert-container > div.nNote').addClass('dn');
            }

            console.log(JSON.stringify(data.model));
            $.ajax({
                data: JSON.stringify(data.model),
                cache: false,
                type: 'post',
                url: 'Respondents/DownloadCSV',
            });
        }
        else {
            errorDialog();
        }
    },
    error: errorDialog,
    complete: function () {
        //unhide cold list / filters, hide loader div.
        $('#loader').addClass('dn');
    }
});

型号:

public class RespondentCoreDataViewModel
    {
        public RespondentsData[] RepondentsCore { get; set; }
    }

    public class RespondentsData
    {
        public string Class { get; set; }
        public Data Data { get; set; }
        public int Id { get; set; }
        public string Url { get; set; }
        public object HtmlAttributes { get; set; }
        public bool IsLocked { get; set; }
    }

    public class Data
    {
        public string Name { get; set; }
        public string Source { get; set; }
        public object Email { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Gender { get; set; }
        public string Ethnicity { get; set; }
        public string Age { get; set; }
        public string LastRecruiter { get; set; }
        public string OwningRecruiter { get; set; }
        public string RecruitingGroup { get; set; }
        public string LastActionDate { get; set; }
        public string Status { get; set; }
        public string _class { get; set; }
        public string url { get; set; }
        public int Id { get; set; }
        public long created { get; set; }
    }

控制器方法:

[HttpPost]
        public ActionResult DownloadCSV(RespondentCoreDataViewModel model )
        {
            ....random code in here
        }

最后Json String引起任何人的兴趣:

[{"Class":"respondent clickable gradeA","Data":{"Name":"John Keys","Source":"<span style=\"display:none;\" >Database</span><i class=\"tipN icon-tasks\" original-title=\"Database\"></i>","Email":null,"City":"","State":"","Gender":"","Ethnicity":"","Age":"","LastRecruiter":"Ben Mabry","OwningRecruiter":"Ben Mabry","RecruitingGroup":"Board 2- Competitive Users","LastActionDate":"<span class=\"dn\">20131008175555</span>10/8 5:55 PM","Status":"<span style=\"display:none;\" >Completed</span><i class=\"tipN icon-check\" original-title=\"Completed\"></i>","class":"respondent clickable gradeA","url":"/Projects/644/Respondents/260619/Overview","Id":260619,"created":1386796133808},"Id":260619,"Url":"/Projects/644/Respondents/260619/Overview","HtmlAttributes":null,"IsLocked":false},{"Class":"respondent clickable gradeA","Data":{"Name":"Jim Walser","Source":"<span style=\"display:none;\" >Database</span><i class=\"tipN icon-tasks\" original-title=\"Database\"></i>","Email":null,"City":"","State":"","Gender":"","Ethnicity":"","Age":"","LastRecruiter":"Ben Mabry","OwningRecruiter":"Ben Mabry","RecruitingGroup":"Board 2- Competitive Users","LastActionDate":"<span class=\"dn\">20131008175529</span>10/8 5:55 PM","Status":"<span style=\"display:none;\" >Completed</span><i class=\"tipN icon-check\" original-title=\"Completed\"></i>","class":"respondent clickable gradeA","url":"/Projects/644/Respondents/260620/Overview","Id":260620,"created":1386796133808},"Id":260620,"Url":"/Projects/644/Respondents/260620/Overview","HtmlAttributes":null,"IsLocked":false},{"Class":"respondent clickable gradeA","Data":{"Name":"Jill Lellis","Source":"<span style=\"display:none;\" >Database</span><i class=\"tipN icon-tasks\" original-title=\"Database\"></i>","Email":null,"City":"","State":"","Gender":"","Ethnicity":"","Age":"","LastRecruiter":"Luanne Jones","OwningRecruiter":"Luanne Jones","RecruitingGroup":"Board 1-Mission Brand","LastActionDate":"<span class=\"dn\">20131009110258</span>10/9 11:02 AM","Status":"<span style=\"display:none;\" >Completed</span><i class=\"tipN icon-check\" original-title=\"Completed\"></i>","class":"respondent clickable gradeA","url":"/Projects/644/Respondents/260621/Overview","Id":260621,"created":1386796133808},"Id":260621,"Url":"/Projects/644/Respondents/260621/Overview","HtmlAttributes":null,"IsLocked":false},{"Class":"respondent clickable gradeA","Data":{"Name":"Charlie Neese","Source":"<span style=\"display:none;\" >Database</span><i class=\"tipN icon-tasks\" original-title=\"Database\"></i>","Email":null,"City":"","State":"","Gender":"","Ethnicity":"","Age":"","LastRecruiter":"Luanne Jones","OwningRecruiter":"Luanne Jones","RecruitingGroup":"Board 2- Competitive Users","LastActionDate":"<span class=\"dn\">20131009112301</span>10/9 11:23 AM","Status":"<span style=\"display:none;\" >Completed</span><i class=\"tipN icon-check\" original-title=\"Completed\"></i>","class":"respondent clickable gradeA","url":"/Projects/644/Respondents/260622/Overview","Id":260622,"created":1386796133808},"Id":260622,"Url":"/Projects/644/Respondents/260622/Overview","HtmlAttributes":null,"IsLocked":false},{"Class":"respondent clickable gradeA","Data":{"Name":"Tara Reid","Source":"<span style=\"display:none;\" >Database</span><i class=\"tipN icon-tasks\" original-title=\"Database\"></i>","Email":null,"City":"","State":"","Gender":"","Ethnicity":"","Age":"","LastRecruiter":"Luanne Jones","OwningRecruiter":"Luanne Jones","RecruitingGroup":"Board 2- Competitive Users","LastActionDate":"<span class=\"dn\">20131009112221</span>10/9 11:22 AM","Status":"<span style=\"display:none;\" >Completed</span><i class=\"tipN icon-check\" original-title=\"Completed\"></i>","class":"respondent clickable gradeA","url":"/Projects/644/Respondents/260623/Overview","Id":260623,"created":1386796133808},"Id":260623,"Url":"/Projects/644/Respondents/260623/Overview","HtmlAttributes":null,"IsLocked":false}]

5 个答案:

答案 0 :(得分:2)

我实际上有一个similar problem.

我的解决方案是在街区:

success: function () {
//code here
}

您需要在此处输入您的成功函数的名称。例如:

success{
//code here
}

ajaxSuccess(result)
{
//code here
}

答案 1 :(得分:1)

你很亲密。我认为这是因为你的action方法需要一个名为“model”的命名参数,你需要这样做:

$.ajax({
data: {model: model},
...

我认为如果你添加它,你的ajax请求将以自动映射到你的action方法参数的格式发送数据。

顺便说一下,如果你重命名其中一个参数,数据会更好:{model:model}不是很好的做法IMO我会重命名你的js变量名,为了清晰起见:

$.ajax({
data: {model: jsModel},

答案 2 :(得分:0)

尝试使用PM控制台将Newtonsoft.Json添加到您的项目中:

install-package Newtonsoft.Json

并重建您的解决方案。

答案 3 :(得分:0)

假设发送的JSON(来自ajax调用中的data: model的模型)有效,我会像Roman建议的那样...将Newtonsoft.Json添加到您的项目中。

在处理JSON而不是ActionResult时,您还应该使用JsonResult方法:

public JsonResult DownloadCSV(RespondentCoreDataViewModel model )
{
   // blah blah
}

答案 4 :(得分:0)

我找到了答案。我不得不改变我的ajax脚本看起来像这样,它只是工作!哦,忽略那些用于测试的成功和错误。

$.ajax({
                        data: JSON.stringify(data.model),
                        contentType: 'application/json',
                        type: 'POST',
                        url: 'Respondents/DownloadCSV',
                        success: function () { alert('success'); },
                        error: function () { alert('unsuccessful'); },
                    });