I've been facing an issue with my JSON payload not getting binded in my controller. I attempted creating a class with
List<Models.UpdateChatRequestModel> Chats
, but that didn't work for me. I also tried using an array name, but that approach seemed to fail as well.
Controller:
[HttpPost]
public IActionResult UpdateChatRequest(IList<Models.UpdateChatRequestModel> request)
{
var model = new Models.ChatModel();
return Json(model);
}
Model:
public class UpdateChatRequestModel
{
public int UserID
{
get; set;
}
public int LastID
{
get; set;
}
}
JavaScript:
class Chat {
constructor(UserID, LastID) {
this.UserID = UserID;
this.LastID = LastID;
}
}
var chats = [new Chat(0, 1), new Chat(1, 4)];
function RequestChatUpdate() {
$.ajax({
type: "POST",
url: '/Chat/UpdateChatRequest',
data: JSON.stringify(chats),
contentType: "application/json",
success: function (data) {
alert("got response from chat update");
}
});
}
JSON sent from RequestChatUpdate()
:
[{"UserID":0,"LastID":1},{"UserID":1,"LastID":4}]