Is this an ASP.NET MVC 2 Preview 1 error, and what is the work?

I use this code in ASP.NET MVC 2 preview 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);
    }

When I use a valid URL argument, such as "/ Comment / List / 22638", I get an error message:

The parameter dictionary contains an invalid entry for the 'commentId' parameter for the "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.Nullable1 [System.Int64]" method . Parameter name: parameters

If I changed the declaration to:

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

The code is working fine. Is this what I'm doing wrong, or is the problem that the reflection is too strict for Int32 vs Int64? And what can I do to fix this? Insert a long line?

+3
3

, / , 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);
+1

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

Just add the following to the global.asax Application_Start method

ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();

For more information, see Scott's blog: 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

Also see jQuery Sort and MVC Stopped Working

+1
source

Source: https://habr.com/ru/post/1714586/


All Articles