Home > database >  i can not pass parameters to backend ( from Ajax to IActionResult or JsonResult) .net core 5
i can not pass parameters to backend ( from Ajax to IActionResult or JsonResult) .net core 5

Time:05-07

i have problem i can't pass params to backend use fech or ajax call , but from another PC i can pass params, here is a sample from code

Backend :

    [HttpPost]
    public IActionResult SubProducat(int Pid)
    {
        List<Lookup> SubProducat = new BAL.Lookups.LookupBL().SubProducatByProduct(Pid);
        return Json(SubProducat);
    }

** Here the Pid always zero **

JavaScript Code:

   $.ajax({
                async: false,
                type: "POST",
                url: '@Url.Action("GetSubProducat", "Notification")',
                data: JSON.stringify({ Pid: $("#ProducatId").val() }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var returnedData = $.parseJSON(data);
                    $('#SubProducatId').empty();
                    $('#SubProducatId').append("<option value='' selected>---Select---</option>");
                    $.each(returnedData, function (key, value) {
                        $('#SubProducatId').append(
                            '<option value="'   value.ID   '">'
                              value.NameEN  
                            '</option>');
                    });
                },
                error: function (Edata) { alert(Edata); }
            }); 

CodePudding user response:

Try the below code

$.ajax({
   type: "POST",
   url: '@Url.Action("SubProducat", "ControllerName")',
   data: $("#ProducatId").val() ,
   contentType: "application/x-www-form-urlencoded",
   dataType: "json",
   success: function (data) {
        ...
   },
   error: function (Edata) { alert(Edata); }
}); 
  • Related