模型在多部件页回发上为空
本文关键字:多部 模型 | 更新日期: 2023-09-27 18:36:39
很抱歉标题很差。
我正在尝试制作一个恢复密码页面。我打算根据模型信息将这一切作为一个具有多个状态的单个页面来完成(也许这不是一个好方法)。
恢复有 4 个步骤
- 验证用户输入的电子邮件
- 验证用户输入的答案安全问题
- 验证用户输入的答案以获得第二种安全性问题
-
显示消息,指出已发送带有链接的电子邮件以重置密码。
[AllowAnonymous] public ActionResult RecoverPassword() { return View(new RecoverPassword()); } [AllowAnonymous] [HttpPost] public ActionResult RecoverPassword(RecoverPassword model) { var user = new Identity("", ""); ASResponse<string> asr; switch (model.View) { case "One": try { asr = Services.ValidateSecurityQuestionOne(user.Token, model.Email, model.Answer); model.View = "Two"; model.Question = "Your Other Favorite Color"; //asr.Payload; model.Answer = string.Empty; } catch (Exception) { ModelState.AddModelError("Question", "Incorrect Answer"); } break; case "Two": try { asr = Services.ValidateSecurityQuestionTwo(user.Token, model.Email, model.Answer); model.View = "Email"; model.Question = string.Empty; } catch (Exception) { ModelState.AddModelError("Question", "Incorrect Answer"); } break; case "Email": break; default: if (!string.IsNullOrEmpty(model.Email)) { try { asr = Services.ValidateEmail(user.Token, model.Email); model.View = "One"; model.Question = asr.Payload; } catch (Exception) { ModelState.AddModelError("Email", "Invalid Email"); } } break; } return View(model); } <div> <div>Retrieve your password</div> @{ using (@Html.BeginForm()) { @Html.HiddenFor(m => m.View) switch (Model.View) { case "One": case "Two": @Html.HiddenFor(m => m.Email) <h3>@Model.Question</h3> @Html.TextBoxFor(m => m.Answer) <br/> <input type="submit" value="Next" /> break; case "Email": <h3>Your email is on its way!</h3> break; default: <h3>Please enter your Account Email Address</h3> @Html.TextBoxFor(m => m.Email) <br/> <input type="submit" value="Next" /> break; } } } </div>
通过上述内容,我输入电子邮件,我得到第一个问题,但是当我提交该答案时,模型。视图为空,因此它返回到电子邮件问题。
创建这样的设置的适当方法是什么?或者如何让我的模型始终具有视图和电子邮件?
尝试删除视图中的 @Html.HiddenFor(m => m.View),并将其移动到每个开关条件中,如下所示:
switch (Model.View)
{
case "One":
@Html.HiddenFor(m => m.Email)
<h3>@Model.Question</h3>
@Html.TextBoxFor(m => m.Answer)
@Html.HiddenFor(m => m.View)
<br/>
<input type="submit" value="Next" />
break;
case "Two":
@Html.HiddenFor(m => m.Email)
<h3>@Model.Question</h3>
@Html.TextBoxFor(m => m.Answer)
@Html.HiddenFor(m => m.View)
<br/>
<input type="submit" value="Next" />
break;
case "Email":
<h3>Your email is on its way!</h3>
break;
default:
<h3>Please enter your Account Email Address</h3>
@Html.TextBoxFor(m => m.Email)
<br/>
<input type="submit" value="Next" />
break;
}