'Ajax call doesn't pass string to controller
I am trying to pass a string to an IActionResult function in an asp.net core 3.1 controller.
Ajax:
$(function () {
$('#ddlUsers').change(function () {
$.ajax({
url: '@Url.Action("GetUserInfoPartial", "UserInfo")',
method: 'POST',
data: { userId: 'test' },
contentType: 'application/json',
dataType: 'html'
}).done(function (r) {
$('#partialID').html(r);
});
});
});
C#:
[HttpPost]
public IActionResult GetUserInfoPartial(string userId)
{
return PartialView("_UserPartial", userId);
}
Breakpoints in GetUserInfoPartial are hit, but userId is always null.
What am I missing?
Solution 1:[1]
you can remove 'application/json', and dataType: 'html' then it will work properly. $.ajax({ url: '@Url.Action("GetUserInfoPartial", "UserInfo")', method: 'POST', data: { userId: 'test' }, success: function (r) { $('#partialID').html(r); });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maryam Yavarifard |
