我在 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.Nullable
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[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/