简体   繁体   English

嵌套JSON值未绑定MVC

[英]Nested JSON values not binding MVC

I have a game object in client side JavaScript that looks like this right before being sent to the server: 在客户端JavaScript中有一个游戏对象,在发送到服务器之前看起来像这样:

客户端

Here it is server side a second later, note all the properties are filled, and the Questions list is populated with the correct number of question, however the properties of each question are null, whereas on the client side they had the correct values. 在这里是服务器端,稍后请注意,所有属性都已填写,并且“问题”列表中填充了正确数量的问题,但是每个问题的属性为null,而在客户端,它们具有正确的值。

服务器端

Here is the code for the models: 这是模型的代码:

public class Game
{
    public Game()
    {
        Questions = new List<Question>(5);
    }
    public int GameID { get; set; }
    public Guid UserID { get; set; }
    public Guid CurrentGameID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public IEnumerable<Question> Questions { get; set; }
}

public class Question
{
    public int ID { get; set; }
    public string Text { get; set; }
    public IEnumerable<int> Answers { get; set; }
    public int SelectedAnswer { get; set; }
}

And here is how I send the object back to the server: 这是我将对象发送回服务器的方式:

// Send completed game back to server
$.post("Games/CompleteGame", currentGame, function (results)
{
    // display results to user
}

Based on Ek0nomik's comment asking about the content-type, I rewrote my ajax call to set contentType to json: 基于Ek0nomik关于内容类型的评论,我重写了ajax调用,将contentType设置为json:

$.ajax(
    {
        url: "Games/CompleteGame",
        type: "POST",
        data: JSON.stringify(currentGame),
        contentType: "application/json",
        success: function (results)
        {
            // show results to user...
        }

As it turns out, this was all it needed to make it work. 事实证明,这就是使其工作所需的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM