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

Posted

技术标签:

【中文标题】ASP.Net将json传递给动作,但服务器端的数据为空【英文标题】:ASP.Net Passing json to action but null data at server end 【发布时间】:2020-10-12 18:26:24 【问题描述】:

我通过 jquery ajax 将 json 传递给控制器​​动作,但是当我调试动作时,我注意到 Section 有所有空值,但是当我检查 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 中的问题在哪里,在服务器端操作中空数据到达那里。请告诉我需要在哪里修复错误。谢谢

【问题讨论】:

【参考方案1】:

通过以下代码使用 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, 格式,即数据:'您在控制器方法中定义的变量名称',jquery 中的变量

【讨论】:

以上是关于ASP.Net将json传递给动作,但服务器端的数据为空的主要内容,如果未能解决你的问题,请参考以下文章

如何将json字符串传递给webmethod c# ASP.NET

如何使用 json 将复杂类型传递给 ASP.NET MVC 控制器

通过 JSON 对象将 Session 对象中的字节数组传递给 Web 服务(Asp.Net 2.0 asmx)

通过 Ajax 将布尔值传递给 asp.net api 控制器

ASP.NET MVC我想重定向到另一个将我的模型传递给它的动作,以获得正确的URL

在 ASP.net MVC 4 中将类对象从一个控制器动作传递到不同的控制器动作