3

我在 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”)时,我收到错误消息:

参数字典包含方法“System.Web.Mvc.ActionResult List(Int64, System.Nullable 1[System.Int64]”的参数“commentId”的无效条目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。参数名称:参数

如果我将声明更改为:

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

代码运行良好。这是我做错了什么,还是反射对于 Int32 和 Int64 类型过于严格的问题?我能做些什么来解决它?投长如弦?

4

3 回答 3

1

尝试

 public ActionResult List(long id, [DefaultValue((long)0)] long? commentId)
于 2009-08-07T07:18:03.477 回答
1

我认为这更具可读性/不那么混乱,并且也适用于 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);
于 2009-08-07T12:43:32.427 回答
1

只需将以下内容添加到 global.asax Application_Start 方法中

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 排序和 MVC 停止工作

于 2011-07-23T18:59:31.497 回答