gh9
gh9

Reputation: 10703

Post JSON to mvc Controller sending null/default value not value I passed in

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

Answers (1)

Jonesopolis
Jonesopolis

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

Related Questions