我想要做一個映射KnockoutJS
模型的帖子。我可以在調試時看到,JSON
是正確的。但服務器顯示Product
爲0(空)。雖然它確實包含1個項目。POST KnockoutJS數據到MVC控制器不綁定
MVC Controller
:
[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'嗎? –
我們使用ko.dataFor @stackoverflow.com/questions/14968565/does-ko-datafor-work-with-select-elements –
@JoffreyKern,'MyModel' added – Quoter