代码之家  ›  专栏  ›  技术社区  ›  John Kaster

这是一个ASP.NET MVC 2 Preview 1 bug吗?解决方法是什么?

  •  3
  • John Kaster  · 技术社区  · 17 年前

    我在ASP.NET MVC 2预览1中使用此代码:

        public ActionResult List(long id, [DefaultValue(0)] long? commentId)
        {
            var model = UserComment.ForRefId(id);
            PageTitle = string.Format("Comments for '{0}'", 
                                      SetCommentThread(id).Subject);
            ViewData[MvcApplication.SAnchor] = commentId;
            return View("List", model);
        }
    

    当我使用有效的url参数(如“/comment/list/22638”)时,我得到错误:

    参数字典包含 参数条目无效 方法的“commentID” 'system.web.mvc.actionResult'系统.web.mvc.actionResult 列表(64) 可空系统 1[System.Int64])' in 'ThreadsMVC.Controllers.CommentController'. The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type 'System.Nullable 1[系统.int64]'。 参数名称:参数

    如果我将声明更改为:

        public ActionResult List(long id, [DefaultValue(0)] int? commentId)
    

    代码运行良好。这是我做错了什么,还是反射对Int32和Int64太严格的问题?我能做些什么来修复它?把长的铸成细绳?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Craig Stuntz    17 年前

    我认为这更易读/不那么混乱,并且在MVC 1中也可以工作:

    public ActionResult List(long id, long? commentId)
    {
        var model = UserComment.ForRefId(id);
        PageTitle = string.Format("Comments for '{0}'", 
                                  SetCommentThread(id).Subject);
        ViewData[MvcApplication.SAnchor] = commentId.GetValueOrDefault(0);
    
        2
  •  1
  •   Spencer Ruport    17 年前

    尝试

     public ActionResult List(long id, [DefaultValue((long)0)] long? commentId)
    
        3
  •  1
  •   Community Mohan Dere    9 年前

    只需将以下内容添加到global.asax应用程序启动方法

    ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
    

    有关更多信息,请参阅Scott的博客: http://weblogs.asp.net/scottgu/archive/2010/12/14/update-on-asp-net-mvc-3-rc2-and-a-workaround-for-a-bug-in-it.aspx

    也看到 jQuery Sort and MVC Stopped Working