Home > OS >  Action parameter getting value null while sending it from ajax is ok
Action parameter getting value null while sending it from ajax is ok

Time:12-15

I have a problem in ASP.NET Core, A action parameter that is sent through using jQuery ajax problem is that data sending from ajax is ok but in the action parameter, it's getting null.

The code snippet is like that

jQuery ajax code is like

   function SaveOrderData() {
        alert("start1");
        var rowitem = "";

       // var OrderList = [];
        var OrderList = new Array();
         $("#mytable tr").each(function () {
            var Order = {};
            var self = $(this);
            Order.OSRC = self.find("td:eq(0)").text().trim();
            Order.ICOD = self.find("td:eq(3)").text().trim();
            Order.TRCD = self.find("td:eq(4)").text().trim();
            Order.QNTY = self.find("td:eq(5)").text().trim();
            Order.RATE = self.find("td:eq(6)").text().trim();
            Order.ARAT = self.find("td:eq(7)").text().trim();
            Order.UNIT = $("#DDUNIT").val();
            Order.DVCD = $("#DDDVCD").val();
            Order.DBCD = $("#DDSALMAN").val();
            Order.ORDN = $("#TXTORDN").val();
            Order.PCOD = $("#DDPCOD").val();
            Order.TXCD = $("#DDTXCD").val();
            Order.AGCD = $("#DDAGCD").val();
            Order.ORDT = $("#TXTORDT").val();
            Order.PORD = $("#TXTPORD").val();
            OrderList.push(Order); // add order object to list object
         });
        alert(JSON.stringify(OrderList));
            $.ajax({
            url: "@Url.Action("SaveOrder", "SaleOrder")",
            type: "GET",
           // data: JSON.stringify(OrderList),
            data: { pendingTestResult: OrderList },
            contentType: "application/json",
            dataType: "json",
            success: function (response) {
          
                if (response == true) {
                // //   ShowMsn("Order have been saved successfully."); // show success notification
                //   // ClearForm(); //clear form fields
                //    $('#mytable').empty(); // clear table items
                //   // CheckSubmitBtn(); // disable submit button
                //} else {
                //  //  ShowMsn("An error has ocurrer while processing the Transaction.");
                 }
            }
        });

    };

controller method is

public JsonResult SaveOrder(List pendingTestResult)

    {
        foreach (ORDMAN ordman in pendingTestResult)
        {
            _db.ORDMAN.Add(ordman);
        }
        _db.SaveChanges();
        

        bool Result = true;
        return Json(Result);
    }

CodePudding user response:

As you want to pass json type data to action,you can try to use POST request in ajax:

js:

function SaveOrderData() {
        alert("start1");
        var rowitem = "";

       // var OrderList = [];
        var OrderList = new Array();
         $("#mytable tr").each(function () {
            var Order = {};
            var self = $(this);
            Order.OSRC = self.find("td:eq(0)").text().trim();
            Order.ICOD = self.find("td:eq(3)").text().trim();
            Order.TRCD = self.find("td:eq(4)").text().trim();
            Order.QNTY = self.find("td:eq(5)").text().trim();
            Order.RATE = self.find("td:eq(6)").text().trim();
            Order.ARAT = self.find("td:eq(7)").text().trim();
            Order.UNIT = $("#DDUNIT").val();
            Order.DVCD = $("#DDDVCD").val();
            Order.DBCD = $("#DDSALMAN").val();
            Order.ORDN = $("#TXTORDN").val();
            Order.PCOD = $("#DDPCOD").val();
            Order.TXCD = $("#DDTXCD").val();
            Order.AGCD = $("#DDAGCD").val();
            Order.ORDT = $("#TXTORDT").val();
            Order.PORD = $("#TXTPORD").val();
            OrderList.push(Order); // add order object to list object
         });
        alert(JSON.stringify(OrderList));
            $.ajax({
            url: "@Url.Action("SaveOrder", "SaleOrder")",
            type: "POST",
           data: JSON.stringify(OrderList),
            contentType: "application/json",
            dataType: "json",
            success: function (response) {
          
                if (response == true) {
                // //   ShowMsn("Order have been saved successfully."); // show success notification
                //   // ClearForm(); //clear form fields
                //    $('#mytable').empty(); // clear table items
                //   // CheckSubmitBtn(); // disable submit button
                //} else {
                //  //  ShowMsn("An error has ocurrer while processing the Transaction.");
                 }
            }
        });

    };

action:

[HttpPost]
        public IActionResult SaveOrder([FromBody]List<Order> OrderList) {
            foreach (ORDMAN ordman in pendingTestResult)
            {
                _db.ORDMAN.Add(ordman);
            }
            _db.SaveChanges();


            bool Result = true;
            return Json(Result);
        }

CodePudding user response:

I tried according this answser ..but not working file attached enter image description here

Parameter Null

  • Related