在多部分页面回发模型空

时间:2013-10-22 22:11:57

标签: c# asp.net-mvc-4 postback

对不起的头衔感到抱歉。

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

恢复有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>
    
  5. 有了上面的内容,我输入了电子邮件,我收到了第一个问题,但是当我提交答案时,model.View为空,因此它会回到电子邮件问题。

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

1 个答案:

答案 0 :(得分:0)

尝试在视图中删除@ Html.HiddenFor(m =&gt; 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;
}