我正在尝试发布映射的
这是一个部分的
KnockoutJS
模型。我可以在调试时看到,JSON
是正确的。但是服务器显示 Product
为0(空)。虽然它包含了1个项目。
MVC 控制器
:[HttpPost]
public ActionResult Test(MyModel model, FormCollection fc)
{
return RedirectToAction("index");
}
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 { }
}
});
});
这是一个部分的
JSON
对象:"Products":[{"Id":2,"Name":"bread"}]
我做错了什么?
编辑:
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; }
}
MyModel
吗? - Joffrey KernMyModel
已添加。 - Quoterjavascript
模型。将TypeId
替换为Id
或反之亦然。 - Shuhel Ahmed