I have the following javascript function which posts data to my controller (asp .net core 3.1 project):
function saveState() {
const gridInstance = $("#ClaimsGrid").dxDataGrid("instance");
var userstate = gridInstance.state();
var data = { StateTypeID: 1, StateChange: userstate };
$.ajax({
type: "POST",
data: JSON.stringify(data),
url: "/api/PostUserState",
contentType: "application/json",
success: function () {
alert("Your grid profile has been saved");
},
error: function () {
alert("An error occurred during save of your grid profile");
}
});
}
My controller is looking for the model UserStateChange which is defined as:
public class UserStateChange
{
public int StateTypeID { get; set; }
public string StateChange { get; set; }
}
The controller code itself is:
[HttpPost("/api/PostUserState")]
public bool PostUserState([FromBody] UserStateChange state)
{
var UserID = new Guid(HttpContext.Session.GetString("UserId").ToString());
UserState mod = new UserState();
var cur = _context.UserState.First(a => a.AppUserID == UserID && a.StateTypeID == state.StateTypeID);
//... more logic here
return true;
}
I'm getting the JSOn.Stringify data correctly in the javascript, but I get a 400 error and never even hit my breakpoint on the controller. What am I doing wrong, why am I getting a 400 error? Why isn't it recognizing my json model correctly?