【问题标题】:MVC3 RC2 JSON Post Binding not working correctlyMVC3 RC2 JSON 后绑定无法正常工作
【发布时间】:2011-01-07 16:49:34
【问题描述】:

我已经看过关于这个主题的其他帖子,并修改了一些变体,但仍然无法让 JSON 模型绑定正常工作。

我的 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; }
}

控制器方法可以正常触发,但具有默认的 UserLoginViewModel 对象,其中 UserName = null、Password = null 和 Persist = false;签名看起来像这样:

[HttpPost]
public ActionResult Logon(UserLoginViewModel model) {
    if (ModelState.IsValid) { 
    ...

【问题讨论】:

    标签: asp.net-mvc json model-binding


    【解决方案1】:

    问题出在客户端!我没有设置 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"); }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2021-06-25
      • 2021-11-08
      • 2017-02-09
      相关资源
      最近更新 更多