我在客户端 JavaScript 中有一个游戏对象,在发送到服务器之前它看起来像这样:
一秒钟后这里是服务器端,请注意所有属性都已填充,并且问题列表中填充了正确的问题数量,但是每个问题的属性均为空,而在客户端,它们具有正确的问题数值。
这是模型的代码:
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; }
}
以下是我如何将对象发送回服务器:
// Send completed game back to server
$.post("Games/CompleteGame", currentGame, function (results)
{
// display results to user
}
最佳答案
根据 Ek0nomik 询问内容类型的评论,我重写了 ajax 调用以将 contentType 设置为 json:
$.ajax(
{
url: "Games/CompleteGame",
type: "POST",
data: JSON.stringify(currentGame),
contentType: "application/json",
success: function (results)
{
// show results to user...
}
事实证明,这就是让它工作所需的一切。
关于javascript - 嵌套 JSON 值不绑定(bind) MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981091/