我已经看过关于这个主题的其他帖子,并修改了一些变体,但仍然无法让 JSON 模型绑定(bind)正常工作。
我的 global.asax.cs Application_Start 方法中有以下内容:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
回发数据如下所示:
{"UserName":"Mike","Password":"password","Persist":true}
我的PoCo:
public class UserLoginViewModel {
public string UserName { get; set; }
public string Password { get; set; }
public bool Persist { get; set; }
}
Controller 方法可以正常触发,但具有默认的 UserLoginViewModel 对象,其中 UserName = null、Password = null 和 Persist = false;签名如下所示:
[HttpPost]
public ActionResult Logon(UserLoginViewModel model) {
if (ModelState.IsValid) {
...
最佳答案
问题出在客户端!我没有设置 contentType。
$.ajax({
url: location.href,
type: "POST",
data: ko.toJSON(this),
datatype: "json",
**contentType: "application/json charset=utf-8",**
success: function (data) { alert("success"); },
error: function (data) { alert("error"); }
});
关于asp.net-mvc - MVC3 RC2 JSON 后绑定(bind)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627985/