Asp.net mvc 这是ASP.NET MVC 2预览版1的一个bug吗;什么是工作?

Asp.net mvc 这是ASP.NET MVC 2预览版1的一个bug吗;什么是工作?,asp.net-mvc,dictionary,parameters,preview,Asp.net Mvc,Dictionary,Parameters,Preview,我在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}'", SetCom

我在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 列表(Int64, 中的System.Nullable
1[System.Int64])
“ThreadsMVC.Controllers.CommentController”。
字典包含的值为
键入'System.Int32',但参数
需要类型为的值
'System.Nullable
1[System.Int64]'。 参数名称:参数

如果我将声明更改为:

    public ActionResult List(long id, [DefaultValue(0)] int? commentId)
代码运行良好。这是我做错了什么,还是反射类型对Int32和Int64太严格的问题?我能做些什么来修复它?将长字符串转换为字符串?

试试看

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

我认为这更具可读性/不太凌乱,在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);

只需将以下内容添加到global.asax应用程序\u Start方法

ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
有关这方面的更多信息,请参阅Scott的博客:


另请参见方便的速记:[DefaultValue(0L)]这在MVC1中对我很有用。你能在MVC1中重现错误吗?还没有尝试MVC 1。。因为这是一个新的项目,我“展望未来”并且非常享受新的模板支持,所以我不想回到MVC 1。这是一个使MVC更吸引我的版本:)我的意思是,我还没有在MVC 1中测试过它。谢谢Craig(和Spencer)。我还需要更新我的路由以支持第二个ID,我现在已经可以使用了。