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

  • Question

  • User-454825017 posted

    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 have to change in code.

    Monday, June 22, 2020 5:53 PM

Answers

  • User-454825017 posted
     
    i missed this in ajax contentType: 'application/json; charset=utf-8', after adding this one now everything is fine now.
     
    here is ajax code which properly send data to action and from action i have seen data is coming instead of null
        $.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);
            }
       });
    issue closed. 
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 9:35 AM