asp.net-mvc - 这是 ASP.NET MVC 2 Preview 1 的错误吗?解决方法是什么?

标签 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}'", 
                                  SetCommentThread(id).Subject);
        ViewData[MvcApplication.SAnchor] = commentId;
        return View("List", model);
    }

当我使用有效的 URL 参数(例如“/Comment/List/22638”)时,出现错误:

The parameters dictionary contains an invalid entry for parameter 'commentId' for method 'System.Web.Mvc.ActionResult List(Int64, System.Nullable1[System.Int64])' in 'ThreadsMVC.Controllers.CommentController'. The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type 'System.Nullable1[System.Int64]'. Parameter name: parameters

如果我将声明更改为:

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

代码运行良好。这是我做错了什么,还是反射对于 Int32 与 Int64 来说类型过于严格的问题?我能做什么来解决它?将 long 转换为字符串?

最佳答案

我认为这更具可读性/更少困惑,并且也适用于 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);

关于asp.net-mvc - 这是 ASP.NET MVC 2 Preview 1 的错误吗?解决方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1243302/

相关文章:

Python:如何从字典中删除元素并将其作为列表返回?

python - 函数内的字典值和列表值

c# - 能否从 ActionFilter 中获取方法中使用的参数值?

c# - 参数值类型为C#的C#中的交换方法是否无效?

c# - 在ASP.NET中将数据插入sql数据库

c# - CultureInfo 的实例(来自同一文化)根据操作系统而变化

asp.net-mvc - 在 ASP.NET Core 1 中渲染 View 时找不到 BCL 类

asp.net-mvc - 在特定列而不是整个网格上使用 settings.CellEditorInitialize

C++友元类映射

c# - 当我想检查参数的正确值时的圈复杂度