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

问题: I am passing json by jquery ajax to controller action but when i debug the action then i notice Section has all null values but when i inspect json there was value. This...

问题:

I am passing json by jquery ajax to controller action but when i debug the action then i notice Section has all null values but when i inspect json there was value.

This is my javascript code where i push data to js array and create json which pass to action.

    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);

This way i am sending data to server side action

    $.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);
        }
    });

my 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"}
        ]
}

my action where section data is getting null

[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; }

}

when i am passing json then whole object is null at server side. data: JSON.stringify({ sdata: Tuner }),

when passing js object instead of json then sections are getting null data: Tuner,

where is the problem in my js for which null data is getting there at server side action. please tell me where i need to fix the error. thanks


回答1:

Use ajax call with following code :

$.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);
    }
});

Whenever you send data to controller try to use data: {'sdata' : Tuner}, format i.e. data: {'Variable name which you defined in controller method', Variable in jquery}

  • 发表于 2020-06-27 19:18
  • 阅读 ( 117 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除