POST KnockoutJS数据到mvc控制器没有绑定

Quo*_*ter 5 javascript asp.net-mvc json asp.net-mvc-4 knockout.js

我正在尝试做一个映射KnockoutJS模型的帖子.我可以看到调试时,这JSON是正确的.但服务器显示Product为0(空).虽然它确实包含1个项目.

MVC Controller:

[HttpPost]
public ActionResult Test(MyModel model, FormCollection fc)
{
   return RedirectToAction("index");
}
Run Code Online (Sandbox Code Playgroud)

AJAX提交:

$('#btnSubmit').click(function (event) {
        var theModel = ko.mapping.toJSON(viewModel);
        debugger;
        $.ajax({
            type: 'POST',
            url: "@Url.Action("Test", "Home")",
            data: theModel,
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                if (!result.success) {
                    //alert(result.error);
                }
                else { }
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)

这是一个部分JSON对象:

"Products":[{"Id":2,"Name":"bread"}]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑:

public class MyModel
{
   public int User { get; set; }
   public string Address { get; set; }
   public string ZipCode { get; set; }
   public List<Product> Products { get; set; }

}

public class Product
{
   public int Id { get; set; }
   public string Name { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

Quo*_*ter 0

在使用 fiddler 进行更多调查后,结果发现我收到了 500 错误并显示以下消息:

System.MissingMethodException: No parameterless constructor defined for this object.
Run Code Online (Sandbox Code Playgroud)

在模型中添加无参数构造函数后,我仍然收到错误消息。这是因为我的模型中有一些造成的SelectList。这就是为什么它不能绑定到模型。

在这篇SO帖子中找到了解决方案(寻找 Chris S 的答案)。希望其他人在面临这个问题时也能有所帮助。