Reputation: 10703
AJAX
var result = {WinnerUserId: 1, LoserUserId:2};
$.ajax({url:'/User/AddGame',
type:'POST',
data: JSON.stringify(result),
dataType:'json',
contentType: "application/json; charset=utf-8",
});
CONTROLLER
[HttpPost]
public ActionResult AddGame(GameResultModel x)
{
return View();
}
MODEL
public class GameResultModel
{
public int WinnerUserId;
public int LoserUserId;
}
The controller just receives 0 in the values for WinnerUserId and LoserUserId. What am I doing wrong?
Upvotes: 1
Views: 451
Reputation: 25370
Ahh I was getting frustrated trying to figure this one out. Your javascript is solid. I failed to notice that your model isn't exposing properties. This'll fix it:
public class GameResultModel
{
public int WinnerUserId { get; set; }
public int LoserUserId { get; set; }
}
Upvotes: 1