asp.net-mvc - MVC3 RC2 JSON 后绑定(bind)无法正常工作

标签 asp.net-mvc json model-binding

我已经看过关于这个主题的其他帖子,并修改了一些变体,但仍然无法让 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/

相关文章:

c# - 在 ASP.NET 路由中扩展 RouteCollection

c# - Controller 返回FileStreamResult时如何清除\清除模型状态错误?

asp.net-mvc - asp.net MVC中的IAuthenticationFilter接口(interface)实现

java - 从 JDeveloper 中的项目读取 json 文件 - Oracle MAF 项目

json - 将 JSON 传递给 HTTP POST 请求

c# - 具有相同名称的路由参数和 View 模型属性 - 意外行为

c# - 对方法参数的 BindAttribute 进行单元测试

java - 模型绑定(bind)在使用 Lombok 的 Springboot 中不起作用

c# - Entity Framework with LINQ 是否符合此 STIG?

java - 在 Spring MVC 中使用 Jackson JSON 解析 JSON