简体   繁体   English

使用EditorFor时被Request.Form覆盖的模型值

[英]Model values overridden by Request.Form when using EditorFor

I am building a wizard - a series of form steps. 我正在构建一个向导-一系列表单步骤。

Each step of the wizard are submitted to the same action method (it needs to be highly extendable), and all step viewmodels inherit from the same base model. 向导的每个步骤都将提交给相同的操作方法(它必须具有高度的可扩展性),并且所有步骤视图模型都从相同的基本模型继承。 They are bound to their concrete type by a custom model binder. 它们通过自定义模型绑定器绑定到其具体类型。 The submit action saves the form fields and returns the next step (or the same in case of errors). Submit操作将保存表单字段并返回下一步(如果发生错误,则返回相同的步骤)。

It works well. 它运作良好。 However, at one point the user needs to supply work information. 但是,某一时刻用户需要提供工作信息。 The step immediately following primary work is secondary work information - same form, same properties. 紧随主要工作之后的步骤是次要工作信息-相同的形式,相同的属性。 The viewmodel for the second part inherits from the primary viewmodel without implementing any individual properties (the class definition is empty). 第二部分的视图模型继承自主视图模型,而不实现任何单独的属性(类定义为空)。

After the primary form is submitted, the secondary viewmodel is returned with values reset. 提交主表单后,将返回辅助视图模型,并重置值。 However, for some reason, the form is shown with the first viewmodels values. 但是,由于某些原因,该表单会显示第一个viewmodels值。

And the really strange thing is, the model values are overridden by Request.Form values for all EditorFor calls, but not for specific calls: 真正奇怪的是,对于所有EditorFor调用,其模型值均被Request.Form值覆盖,但对于特定调用,则不:

// This displays correctly, showing the current model values
<div>Model says: @Model.AuthorizationNumber</div>
<div><input type="text" value="@Model.AuthorizationNumber"/></div>

// This displays the Request.Form value from the previous step.
@Html.EditorFor(x => x.AuthorizationNumber)

In the above, the authorization number is shown correctly for the first two lines (constructor values), but the third is incorrect (showing the Request.Form value). 在上面的代码中,前两行(构造函数值)正确显示了授权号,但第三行不正确(显示Request.Form值)。

What could be the problem? 可能是什么问题呢?

EDIT 1 编辑1

On request, the controller method handling the submit. 根据请求,控制器方法处理提交。

    public ActionResult Index(StepPathState model = null)
    {
        var isPreviousSubmit = Request.Form.AllKeys.Contains("submit-previous");

        if (model == null || model.Step == null) return View(Steps.GetFirstPathResult());

        // submit the step data
        var result = Steps.SubmitStepData(model, isPreviousSubmit);

        if (result.Status is StepSuccessAndFinish)
        {
            return Finish(result);
        }

        var failureStatus = result.Status as StepActionStatusFailure;

        if (failureStatus != null)
        {
            foreach (var error in failureStatus.Errors)
            {
                ModelState.AddModelError(error.Property, error.ErrorMessage);
            }
        }

        return View(result);
    }

I may have found the answer though: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx 我可能已经找到了答案: http : //blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value。 ASPX

Solution: The good and the ugly 解决方案:好与丑

As the article linked above describes, it seems that MVC favors the modelstate values when returning from a POST. 如上面链接的文章所述,从POST返回时,MVC似乎更喜欢modelstate值。 This is because re-displaying the form in a POST should always indicate a validation failure. 这是因为在POST中重新显示表单应始终表示验证失败。 So the correct solution is to save, redirect to a GET request and display the new form. 因此,正确的解决方案是保存,重定向到GET请求并显示新表单。 This would fix the problem, and is what I am going to do when I have time to refactor. 这将解决问题,这是我有时间重构时要做的事情。

A temporary solution is to clear the modelstate. 临时解决方案是清除modelstate。 This will cause it to fallback back to the model. 这将导致它回退到模型。 This is my temporary fix: 这是我的临时解决方法:

if (failureStatus == null && ModelState.IsValid)
{
    ModelState.Clear();
}

return View(result);

The danger here is that I have cleared the modelstate. 这里的危险是我已经清除了模型状态。 And what might that entail in the long run? 从长远来看,这可能会带来什么? New bug adventures to come. 新的错误冒险即将来临。 But I am on the clock. 但是我在计时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM