【问题标题】:Nested JSON values not binding MVC嵌套的 JSON 值不绑定 MVC
【发布时间】:2013-04-12 21:24:35
【问题描述】:

我在客户端 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
}

【问题讨论】:

  • 你能显示在 POST 中发送的原始 JSON 吗?另外,你能验证一下内容类型吗?
  • @Ek0nomik 在这种情况下我不确定如何获取原始 JSON。

标签: javascript asp.net .net asp.net-mvc-3 model-binding


【解决方案1】:

根据 Ek0nomik 关于内容类型的评论,我重写了我的 ajax 调用,将 contentType 设置为 json:

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

事实证明,这就是让它工作所需要的一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多