将变量添加到HTML.BeginForm模型中

时间:2013-01-11 20:40:04

标签: c# html5 razor asp.net-mvc-4

我在我的GET控制器和我的View之间保留passwordToken时遇到问题。我看到令牌被传递并在GET控制器中正确地添加到模型中,但是一旦HTML.BeginForm在View中启动,模型就会有一个新实例,之前的模型会丢失passwordToken。我需要保留passwordToken才能使用WebSecurity.ResetPassword。关于如何做到这一点的任何建议?

我的GET控制器:

    [AllowAnonymous]
    public ActionResult PasswordReset(string passwordToken)
    {
        // Token Validation
        var usrID = WebSecurity.GetUserIdFromPasswordResetToken(passwordToken);
        var usr = _dbManager.GetUserInformation(usrID);

        if (usr == null)
        {
            //The link you are using is not valid anymore
            return RedirectToAction("Error", "Account");
        }
        else
        {
            var model = new PasswordReset();
            model.PasswordResetToken = passwordToken;
            return View(model);
        }
    }

我的观点:

@model Project.Models.PasswordReset
@{
ViewBag.Title = "Password Reset";
}

<h2>Password Reset</h2>

<div class="form passwordreset-form">


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <div class="input-form">

        <div class="inputbox-label">
            @Html.LabelFor(m => m.Password)
        </div>
        <div class="inputbox">
            @Html.PasswordFor(m => m.Password)
        </div>

        <div class="inputbox-label">
            @Html.LabelFor(m => m.ConfirmPassword)
        </div>
        <div class="inputbox">
            @Html.PasswordFor(m => m.ConfirmPassword)
        </div>

    </div>
    <div style="float:right;">
        <input type="submit" value="Change Password" />
    </div>

}

</div>

我的POST控制器:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult PasswordReset(PasswordReset model)
    {
        //Attemp to change password
        var passwordChangeConfirmation = WebSecurity.ResetPassword(model.PasswordResetToken, model.Password); 

            //Password has been changed
        if(passwordChangeConfirmation == true)
        {
            return RedirectToAction("Index", "Home");
        }
            //Password change has failed
        else
        {
            return RedirectToAction("Error", "Account");
        }
    }

3 个答案:

答案 0 :(得分:3)

我最终调整了POST类以使其正常工作。

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult PasswordReset(PasswordReset model, string passwordToken)
    {
        //Attemp to change password
        model.PasswordResetToken = passwordToken;
        var passwordChangeConfirmation = WebSecurity.ResetPassword(model.PasswordResetToken, model.Password);

        //Password has been changed
        if (passwordChangeConfirmation == true)
        {
            return RedirectToAction("Index", "Home");
        }
        //Password change has failed
        else
        {
            return RedirectToAction("Error", "Account");
        }
    }

答案 1 :(得分:2)

将其添加到您的表单中:

@Html.HiddenFor(m => m.PasswordResetToken);

答案 2 :(得分:1)

您可以在表单上使用您传递的字段(来自您的模型)的隐藏输入。

@Html.HiddenFor(m => m.PasswordResetToken);
输出中的

<input type="hidden" name="PasswordResetToken"></input>