0

抱歉标题不好。

我正在尝试制作一个恢复密码页面。我打算将这一切作为一个具有多个状态的单个页面来完成,具体取决于模型信息(也许这不是一个好方法)。

恢复有 4 个步骤

  1. 验证用户输入的电子邮件
  2. 验证用户输入的第一个安全问题的答案
  3. 验证用户输入的第二个安全问题的答案
  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>
    

使用上述内容,我输入电子邮件,我收到第一个问题,但是当我提交该答案时,model.View 为空,因此它返回到电子邮件问题。

创建这样的设置的适当方法是什么?或者我怎样才能让我的模型始终拥有视图和电子邮件?

4

1 回答 1

0

尝试在视图中删除 @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;
}
于 2013-10-22T23:33:27.300 回答