简体   繁体   English

为什么我的Ajax帖子在进入后端时有内容,但是我的模型和控制器却收到null

[英]Why does my Ajax post have content when going to backend but my model and controller receive null

AJAX Post shows correct content in debugger but my controller and model receive null values. AJAX Post在调试器中显示正确的内容,但是我的控制器和模型接收到空值。

I have changed my ajax post many ways. 我已经以多种方式更改了我的Ajax帖子。 I have also just sent one string and had one string as my controller parameter. 我也刚发送了一个字符串,并将一个字符串作为我的控制器参数。 This worked, but I am looking to send the whole object. 这可行,但是我希望发送整个对象。

        var BuildContent = [{ 'Cal': Cal, 'BarLength': BarLength, 'Color': 
        Color }];
        content = JSON.stringify(BuildContent);
        $.ajax({
            type: "POST",
            url: "../Build/Build",
            data: {'content':content},
            ContentType: "application/json",
            dataType: "json",
        });
    public class BuildController : Controller
    {


        [HttpPost]
        public BuildContent Build (BuildContent content) 
        {
            //BuildService.GetURG(data);
            return content;
        }
    }
    public class BuildContent
    {

        public string Cal { get; set; }
        public string BarLength { get; set; }
        public string Color { get; set; }
    }

SO when the POST reaches the controller I am seeing [Cal = null , BarLength = null, Color = null] Where as in the Chrome network debugger I see my content correctly. 因此,当POST到达控制器时,我会看到[Cal = null,BarLength = null,Color = null]在Chrome网络调试器中,我可以正确看到内容。 Such as content: [{"Cal":"5.56","BarLength":"16\\"","Color":"Black"}] 例如内容:[{“ Cal”:“ 5.56”,“ BarLength”:“ 16 \\”“,” Color“:” Black“}]

You're not sending an array of objects, only a single one. 您不是在发送对象数组,而是仅发送一个对象。 So there's no need to surround your object in an array initialiser. 因此,无需将对象包含在数组初始化器中。

Below I've created a JavaScript object and then converted it into JSON. 在下面,我创建了一个JavaScript对象,然后将其转换为JSON。 Try this and let me know how it goes. 试试这个,让我知道如何进行。

var buildContent = {'Cal': 'Testing123...', 'BarLength': 'Noot Noot', 'Color': 'Red'};
var jsonData = JSON.stringify(buildContent);
$.ajax({
    type: "POST",
    url: "../Build/Build",
    data: jsonData,
    dataType: 'json',
    contentType: 'application/json;'
});

EDIT: Alternatively try sending the object in its entirety, without serializing it to JSON. 编辑:或者尝试完整地发送对象,而不将其序列化为JSON。

var buildContent = {'Cal': 'Testing123...', 'BarLength': 'Noot Noot', 'Color': 'Red'};
$.ajax({
    type: "POST",
    url: "../Build/Build",
    data: buildContent
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM