0

Hello Am Facing problem in Fetching Data and Transferring to Controller

Below is My JQuery Code Using Ajax

$("#save").click(function () {
       
        $.ajax({
            type: 'POST',
            url: '/PCM/PCMPost',
            dataType: "json",
            data: { final: JSON.stringify(final) },
            //data: JSON.stringify({ final: final }),
            success: function (data) {
                alert("Succeded");
            }
        })
    })

intialized like this

var final=[];

final array looks like after push operation, in console and it has expected data to

(2)[{...},{...}] 

controller code

public ActionResult PCMPost(IEnumerable<ProductCategaryMapping> final)
{
      return RedirectToAction("ProductCategaryMappingDisplay", "PCM");
}

while debugging "final" is showing null

i dont know where am going wrong please help me out

Baba Ranjith
  • 196
  • 14
  • check out https://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form – Patrick Hume Jul 04 '22 at 11:29
  • @RoryMcCrossan . by getting the Json data in IEnumerable<>type as parameter in controller `public ActionResult PCMPost(IEnumerable final)` i will use this parameter and store data of this parameter to DB (pass this param to repository to store) however to do this operation . am getting count=0 while debugging (in controller ) so i kept controller empty. note: going inside ajax sucess . only issue is jquery data not getting in controller parameter – Baba Ranjith Jul 04 '22 at 11:47
  • Apologies, I thought you were making a GET, not a POST – Rory McCrossan Jul 04 '22 at 11:49
  • `$.ajax({ type: 'POST', url: '/PCM/PCMPost', data: JSON.stringify({ final: final }), success: function (data) { alert("Succeded"); } })` ya tried . still not working . finding "final null" in controller @RoryMcCrossan please help me out – Baba Ranjith Jul 04 '22 at 12:08
  • `data: JSON.stringify(final),` – Rory McCrossan Jul 04 '22 at 12:10
  • @RoryMcCrossan tried `data: JSON.stringify(final),` ,`data: JSON.stringify({ final: final }), `,`data: { final: JSON.stringify(final) },` all this way tried . still final is null in controller. `{ProductCode: '01', ProductName: 'HIMALAYA ', Division: 'CP', Category: 'HIMALAYA', SubCategory: 'FAIRNESS', …} Capacity: 0 Category: "HIMALAYA" Division: "CP" IsPriorityUpdate: 1 Priority1per: "100" Priority2per: "0" Priority3per: "0" Priority_1: "1145" Priority_2: "" ProdPity: 1 ProductCode: "0011" ProductName: "HIMALAYA FAC CREAM" SubCategory: "FAIRNESS CREAM" [[Prototype]]: Object` – Baba Ranjith Jul 04 '22 at 12:20
  • @RoryMcCrossan after converting to json . data look like above {productCode-------object} – Baba Ranjith Jul 04 '22 at 12:22

1 Answers1

1

Send data without converting it to string.

$("#save").click(function () {
   
    $.ajax({
        type: 'POST',
        url: '/PCM/PCMPost',
        dataType: "json",
        data: { final:final },           
        success: function (data) {
            alert("Succeded");
        }
    })
})
subeesh k
  • 334
  • 2
  • 3