Home > Enterprise >  Fetch Data From jquery (Ajax) to Controller in IEnumerable<--------->
Fetch Data From jquery (Ajax) to Controller in IEnumerable<--------->

Time:07-05

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

CodePudding user response:

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

CodePudding user response:

Remove dataType and put contentType: 'application/json;'. Also change data to JSON.stringify({ final: final })

$.ajax({
   type: 'POST',
   url: '/PCM/PCMPost',
   contentType: 'application/json;',
   data: JSON.stringify({ final: final }),
   success: function (data) {
   alert("Succeded");
  }
})
  • Related