【问题标题】:MVC Won't deserialize HttpGet variablesMVC 不会反序列化 HttpGet 变量
【发布时间】:2012-08-18 11:43:21
【问题描述】:

当我提交这个网址时:

http://localhost:3333/User/GetAll?_dc=1345288777353&page=1&start=1&limit=25&callback=Ext.data.JsonP.callback2 

控制器不会反序列化PagingModel page。调试器显示page = null

public class PagingModel
{

    public string start { get { return _start; } set { _start = value; } }
    private string _start;

}

public class UserController : Controller
{

    [HttpGet]
    public JsonResult GetAll(PagingModel page)
    {
           ///////////////////
           //page is null.
           ///////////////////
    }
}

【问题讨论】:

    标签: asp.net-mvc parameters action


    【解决方案1】:

    糟糕,重命名你的动作参数:

    [HttpGet]
    public JsonResult GetAll(PagingModel model)
    {
       ///////////////////
       // model is no longer null
       ///////////////////
    }
    

    这样做的原因是因为您的请求中已经有一个 page=1 查询字符串参数,这使得默认模型绑定器发疯,他试图将值 1 反序列化为 PagingModel,这显然很难发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 2012-10-31
      • 2011-05-13
      • 2012-07-18
      相关资源
      最近更新 更多