0

This code is being used to send id of an anchor element on its click event from a view to a controller using ajax and jQuery.

<script type="text/javascript">
    $(document).on('click', 'a', function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("/brandsOfACategory")',
            contentType: 'application/json; charset:utf-8',
            data: JSON.stringify(this.id)
        })
    });
</script>

Action Method used to catch this string is:

[HttpPost]
public ActionResult brandsOfACategory(string id)
{
    return View();
}

But string id in this action method is coming as null. I checked Network tab of Chrome developer tool for Request Payload to check the value of the parameter and it is an object and not a string. It's screen is uploaded here: https://i.stack.imgur.com/hE240.jpg

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179

1 Answers1

1

If you trying to send id from ajax call to controller method then you need to pass it as {key: value} pair

So instead of this

data: JSON.stringify(this.id)

Try this

data: JSON.stringify({id: this.id})
er-sho
  • 9,581
  • 2
  • 13
  • 26