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);
    }

「/Comment/List/22638」などの有効な URL 引数を使用すると、次のエラーが発生します。

パラメータ ディクショナリに、メソッド '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 に対してリフレクションの型が厳密すぎるという問題ですか? そして、それを修正するにはどうすればよいですか? long を文字列としてキャストしますか?

4

3 に答える 3

1

これは読みやすく、面倒ではなく、MVC1でも機能すると思います。

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

試す

 public ActionResult List(long id, [DefaultValue((long)0)] long? commentId)
于 2009-08-07T07:18:03.477 に答える
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 Sort と MVC が機能しなくなったも参照してください。

于 2011-07-23T18:59:31.497 に答える