2014-09-24 51 views
0

我有博客,有时用户捕获错误,我不能重现。ASP.NET MVC的Html.Action渲染后,而不是GET

简单PostItem.cshtml:

“创建”, “注释” - 渲染CreateComment形式

@Html.Action("Create", "Comment", new { @returnUrl = Request.Url, @postID = Model.ID }) 
<div name="#comments"> 
    @Html.Action("CommentsForPost", "Comment", new { postID = Model.ID }) 
</div> 

CreateComment.cshtml

<div class="create-comment-container"> 
@using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { role = "form" })) 
{ 
    @Html.Hidden("returnUrl", Request.QueryString["returnUrl"]) 
    @Html.HiddenFor(m => m.PostID) 
    ......... 
    <button type="submit" class="btn btn-default pull-right">@L("Post")</button> 
} 

那么我CommentController。 cs

 public PartialViewResult Create(int postID, string returnUrl) 
     { 
      return this.PartialView("Partials/CreateComment", new Comment { PostID = postID }); 
     } 

     [HttpPost] 
     [ValidateInput(false)] 
     public async Task<ActionResult> Create(Comment comment, string returnUrl) 
     { 
      var validator = ValidationFactory.CreateValidator<Comment>(); 

      var results = validator.Validate(comment); 

      if (results.IsValid) 
      { 
       var result = await CommentManager.CreateComment(comment); 
       if (result != null && !String.IsNullOrWhiteSpace(returnUrl)) 
        return Redirect(returnUrl + "#comments"); 
      } 

      return Redirect("/"); 
     } 

     public PartialViewResult CommentsForPost(int postID) 
     { 
      return PartialView("CommentsForPost", CommentManager.GetComments(postID, PaginationHelper.GetCurrentPageForName(ControllerContext.RequestContext, "commentPage"), BlogSettings.CommentPageSize)); 
     } 

正如你所看到的 - 所有的行动都与重定向一起出去! 但是,有些用户会发现错误,将其重定向到主页面而不保存评论,并且在我的日志中有这个跟踪。 这怎么可能?

HTTP_X_ORIGINAL_URL /评论/创建

REQUEST_METHOD GET

System.ArgumentException 
The parameters dictionary contains a null entry for parameter 'postID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult Create(Int32, System.String)' in 'geenBlog.Web.Controllers.CommentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters 

System.ArgumentException: The parameters dictionary contains a null entry for parameter 'postID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult Create(Int32, System.String)' in 'geenBlog.Web.Controllers.CommentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 
Parameter name: parameters 
    at System.Web.Mvc.ActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary`2 parameters, MethodInfo methodInfo) 
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) 
    at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) 
    at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) 
    at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

回答

0

postID nullable如图所示:

public PartialViewResult Create(int? postID, string returnUrl) 
public PartialViewResult CommentsForPost(int? postID) 

或者如果你不想让postID nullable然后进行确保在0123中有一些整数值当你通过postID@Html.Action()

+0

但Model.ID始终存在,因为我们在博客帖子页面发布自己的ID。在什么情况下,postID不存在? – ZOXEXIVO 2014-09-24 08:54:46

+0

@ ZOXEXIVO..like你写过你的问题有时你会发现这个错误,因为有时候你的Model.ID是null,这会导致问题,因为int donot接受空值...尝试我的第一个答案set int为空或否则只是检查某些地方Model.ID在某些情况下获得了空值。 – 2014-09-24 08:58:40

+0

如果model = null,我们在postId = Model.ID赋值中有空引用异常。 ID字段声明为TKey ID {get;组; }并且不能给null值! – ZOXEXIVO 2014-09-24 11:42:05