1

我有一个 asp.net 4.5 应用程序,它是一个向导 UI,最后会生成一个报告。我正在使用 MVC Futures 插件在表单之间序列化和传递模型。我可以一直点击并生成报告。我还按 F5 并刷新,它将重新发布上一个请求中的表单数据并重新加载表单。

我的问题是有时,通常在等待几分钟后,如果我再次尝试在页面上按 F5,而不是重新生成报告,我会收到以下错误:

SerializationException:反序列化失败。

似乎浏览器可能以某种方式忘记了先前的请求,并试图在不重新提交表单的情况下刷新页面。关于发生了什么的任何想法?我将尝试通过立即重新提交表单来重现此问题,然后等待几分钟并再次执行此操作,并使用 Fiddler 观看每次发布的内容。

有人对这里可能出现的问题有任何想法吗?

更新(2014 年 1 月 3 日):

这是生成报告的 post 方法:

[HttpPost]
public ActionResult GenerateReport([Deserialize] WizardModel wizardModel, string providerId = null, string providerName = null)
{
    var model = new ReportModel();
    if (!String.IsNullOrEmpty(providerId))
    {
        wizardModel.Providers = new List<NameValue>(){ new NameValue() { Name = providerName, Value = providerId }};
        model.AutoPrint = true;
    }

    var param = new MeasureParams()
    {
        Key = wizardModel.CriteriaKeys.ToArray(),
        ProviderIds = (wizardModel.Providers != null ? wizardModel.Providers.Select(x => x.Value).ToArray() : null),
        StartDate = (wizardModel.StartDate),
        StartDateAsString = (wizardModel.StartDate).ToString(),
        EndDate = (wizardModel.EndDate),
        EndDateAsString = (wizardModel.EndDate).ToString()
    };

    var results = _measureService.CalculateMeasures(param);

    model.WizardModel = wizardModel;
    model.Results = new List<List<ResultsModel>>();

    model.Results.AddRange(MeasureResultsMapper.MapMeasurementsResultToHierarchicalList(results));
    if (wizardModel.IsDetailedReport)
    {
        return View("DetailedReport", model);
    }
    return View("SummaryReport", model);                
}

视图页面本身并没有什么特别之处。它甚至没有序列化对象。那是在上一个评论页面中。我可以发布该页面的一部分:

@{ Html.BeginForm("GenerateReport", "Wizard", FormMethod.Post, new {id = "reviewForm"});}
@Html.Serialize("wizardModel", Model)
@{Html.EndForm();}

全栈跟踪:

“/”应用程序中的服务器错误。


你调用的对象是空的。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误:在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例。] System.Web.Security.MachineKey.Decode(String encodedData, MachineKeyProtection protectionOption) +268 Microsoft.Web.Mvc.MachineKeyWrapper.Decode(String encryptedData, MachineKeyProtection protectionOption) + 41 Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode, IMachineKey machineKey) +175

[SerializationException:反序列化失败。验证数据是否正在使用与序列化相同的 SerializationMode 进行反序列化。否则请参阅内部异常。] Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode, IMachineKey machineKey) +384 Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode) +70 Microsoft.Web. Mvc.DeserializingModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +293 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 System.Web.Mvc.Async.<DisplayClass25.b _1e(AsyncCallback asyncCallback, Object asyncState) +446 System.Web.Mvc.Async.WrappedAsyncResult 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调, 对象状态, Int32 超时) +130 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调,对象状态)+381 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback回调,对象状态,Int32超时)+130 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+249 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态)+49 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext 上下文,AsyncCallback cb,对象 extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute( ) +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

4

1 回答 1

1

您使用错误的语法来呈现您的表单。用这个替换你的表格:

@using(Html.BeginForm("GenerateReport", "Wizard", FormMethod.Post, new {id = "reviewForm"}))
{
    @Html.Serialize("wizardModel", Model)
}

不像Html.EndForm(),Html.BeginForm()不是 void 函数。你不能这样称呼它。如果你想像这样呈现表单,你应该像下面那样做,但我不建议这样做,因为它不容易阅读:

@Html.BeginForm()
@Html.Serialize("wizardModel", Model)
@{Html.EndForm();}
于 2014-01-03T20:28:58.693 回答