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)
但Model.ID始終存在,因爲我們在博客帖子頁面發佈自己的ID。在什麼情況下,postID不存在? – ZOXEXIVO 2014-09-24 08:54:46
@ ZOXEXIVO..like你寫過你的問題有時你會發現這個錯誤,因爲有時候你的Model.ID是null,這會導致問題,因爲int donot接受空值...嘗試我的第一個答案set int爲空或否則只是檢查某些地方Model.ID在某些情況下獲得了空值。 – 2014-09-24 08:58:40
如果model = null,我們在postId = Model.ID賦值中有空引用異常。 ID字段聲明爲TKey ID {get;組; }並且不能給null值! – ZOXEXIVO 2014-09-24 11:42:05