ASP.Net 将 json 传递给操作,但服务器端的数据为空

ASP.Net Passing json to action but null data at server end

我通过 jquery ajax 将 json 传递给控制器​​操作,但是当我调试操作时,我注意到部分有所有空值,但是当我检查 json有价值。

这是我的 javascript 代码,我将数据推送到 js 数组并创建 json 传递给操作。

    var Tuner = {};
    Tuner.Tick1 = 'true';
    Tuner.Tick2 = 'false';
    Tuner.Sections = [];

    var Section_LI = {};
    Section_LI.Section='Consensus Model';
    Section_LI.LI = 'Operating Earnings Before Income Taxes';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section='Consensus Model';
    Section_LI.LI = 'Net Income attributable to common, GAAP';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section='Consensus Model';
    Section_LI.LI = 'Diluted Shares Outstanding';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section = 'Test';
    Section_LI.LI = 'Tridip';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section='Consensus Model';
    Section_LI.LI='Operating EPS';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section = 'Segment Details';
    Section_LI.LI = 'Limited Partnership Income-Retirement';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section = 'Segment Details';
    Section_LI.LI = 'Prepayment Fee Income-Retirement';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section = 'Segment Details';
    Section_LI.LI = 'Gross Investment Income-Retirement';
    Tuner.Sections.push(Section_LI);

    Section_LI = {};
    Section_LI.Section = 'Segment Details';
    Section_LI.LI = 'Investment Expenses-Retirement';
    Tuner.Sections.push(Section_LI);

这样我将数据发送到服务器端操作

    $.ajax({
        type: 'POST',
        url: '@Url.Action("Test1", "Home")',
        /*contentType: 'application/json; charset=utf-8',*/
        //data: JSON.stringify({ sdata: Tuner }),
        data: Tuner,
        dataType: 'json',
        /*dataType: 'text',*/
        success: function (response) {
            if (response.success) {
                alert(response.responseText);
            } else {
                // DoSomethingElse()
                alert(response.responseText);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });

我的json

{
    "Tick1":"true",
    "Tick2":"false",
    "Sections":
        [
            {"Section":"Consensus Model","LI":"Operating Earnings Before Income Taxes"},
            {"Section":"Consensus Model","LI":"Net Income attributable to common, GAAP"},
            {"Section":"Consensus Model","LI":"Diluted Shares Outstanding"},
            {"Section":"Consensus Model","LI":"Operating EPS"},
            {"Section":"Segment Details","LI":"Limited Partnership Income-Retirement"},
            {"Section":"Segment Details","LI":"Prepayment Fee Income-Retirement"},
            {"Section":"Segment Details","LI":"Gross Investment Income-Retirement"},
            {"Section":"Segment Details","LI":"Investment Expenses-Retirement"}
        ]
}

我的部分数据变为空的操作

[HttpPost]
public ActionResult Test1(Tuner sdata)
{
    return Json(new { success = true, responseText = "Your message successfuly sent!" }, JsonRequestBehavior.AllowGet);
}

public class Tuner
{
    public string Tick1 { get; set; }
    public string Tick2 { get; set; }
    public List<Sections> Sections { get; set; }

}

public class Sections
{
    public string Section { get; set; }
    public string LI { get; set; }

}

当我传递 json 时,整个对象在服务器端为空。 data: JSON.stringify({ sdata: Tuner }),

当传递 js 对象而不是 json 时,部分将变为空 data: Tuner,

我的 js 中的问题在哪里,空数据在服务器端操作时到达那里。请告诉我我需要在哪里修复错误。谢谢

使用 ajax 调用,代码如下:

$.ajax({
    type: 'POST',
    url: '@Url.Action("Test1", "Home")',
    data: {'sdata' : Tuner},
    dataType: 'json',
    success: function (response) {
        if (response.success) {
            alert(response.responseText);
        } else {
            // DoSomethingElse()
            alert(response.responseText);
        }
    },
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    }
});

每当您向控制器发送数据时,请尝试使用 data: {'sdata' : Tuner}, 格式,即数据:{'Variable name which you defined in controller method',jquery}

中的变量