javascript - 嵌套 JSON 值不绑定(bind) MVC

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

我在客户端 JavaScript 中有一个游戏对象,在发送到服务器之前它看起来像这样:

Client Side

一秒钟后这里是服务器端,请注意所有属性都已填充,并且问题列表中填充了正确的问题数量,但是每个问题的属性均为空,而在客户端,它们具有正确的问题数值。

Server Side

这是模型的代码:

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/

相关文章:

asp.net - Razor View 中当前上下文中不存在资源

.net - 增加的传出连接数

.net - 如何将介绍的 ML.Net 演示翻译成 F#?

javascript - 创建系统为 3 名员工提供积分,每次我单击其中一个按钮时,我都会为所选用户提供 1 分 -

javascript - Apps 脚本边栏上的 Bootstrap Accordion 效果

c# - 如何删除在 ASP.NET MVC4 SimpleMembership 中具有角色的用户?

.net - 索引和长度必须指向字符串中的某个位置吗?

javascript - JointJS - 鼠标点击事件触发单元格位置改变事件

javascript - 如何从第三方库中获取 'Uncaught (in promise) Error'?

c# - WPF - 在没有结构化异常处理的情况下检查资源是否存在