Home > Software design >  Return parameter null in controller from ajax
Return parameter null in controller from ajax

Time:01-16

I don't know what happened the value sent to the controller is always null, even though in the console.log the value is there, please I need help

this is my ajax:

$('#ddlItemName').change(function () {

    var ItemCode = $('#ddlItemName').text();

    if (ItemCode != '') {

        var RequestParam = {
            search: ItemCode
        }

        console.log(RequestParam);

        $.ajax({
            url: '/Order/CustomerItemOrders',
            type: 'POST',
            data: JSON.stringify(RequestParam),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data[0].Name);
            },
            error: function (data) {
                toastr.error(data);
            }
        });
    }

    $('#ddlItemName').text('');
});

This is my controller :

[HttpPost]
public JsonResult CustomerItemOrders([FromBody] string search)
{
  var result = _order.BindCustomerOrders(search);

  return Json(result.data);
}

This is my error : enter image description here

I've tried adding '[FromBody]' but the value parameter still doesn't get sent

CodePudding user response:

I recomend you add the parameter to query

If you insist on pass the value with request body

Try creat a model:

public class SomeModel
{
  public string search{get;set;}
}

in your controller:

public JsonResult CustomerItemOrders([FromBody] SomeModel somemodel)
{
  .......
}
  • Related