繁体   English   中英

如何在一个动作结果中保存临时值对象并在另一个动作结果中使用它

[英]how to hold temporary value object in a actionresult and use it in another actionresult

您好,我想在一个操作方法中保存一个临时代码,例如“代码”,并在另一个操作方法上使用它来比较是否在视图文本框中输入的model.code是“代码”。但是我不想将其保存到数据库中

这是我第一个将保留值的actionresult方法

  [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> PhoneReset(ForgotPasswordView model, string sms)
    {

        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByEmailAsync(model.Email);
            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            if (user != null || user.PhoneNumber==model.cellNumber)
            {
                //string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                //var forgot = new ForgotPasswordBusiness();

                GenerateCodeBusiness gen = new GenerateCodeBusiness();

                var codes = gen.CreateRandomPassword(8);
                SendSmsBusiness objap = new SendSmsBusiness();
                sms = "Your password reset code is " + codes;
                objap.Send_SMS(model.cellNumber, sms);
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("ResetViaPhone", "Account");
            }
            else if (user == null)
            {
                ModelState.AddModelError("", "The user does not exist");
                return View();
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

这是第二个动作结果

        [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
    {


        if (!ModelState.IsValid)
        {
            return View(model);
        }
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null)
        {
            ModelState.AddModelError("", "No user found.");
            return View();
        }
        IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
        if (result.Succeeded)
        {
            return RedirectToAction("ResetPasswordConfirmation", "Account");
        }
        AddErrors(result);
        return View();
    }

这是在其中输入代码的resetPassword视图。

@model Template.Model.ResetPasswordViewModel
@{
ViewBag.Title = "Reset password";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
 @Html.AntiForgeryToken()
<h4>Reset your password.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Code)




<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Reset" />
    </div>
</div>

}

这是resetpassword模型

 public class ResetPasswordViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public string Code { get; set; }

任何帮助都会有很大帮助

使用TempData-

 //Save values in tempData
 TempData["codes"] = codes;

 //Fetching the values in different action method
 //Note: Cast the tempdata value to its respective type, as they are stored as object
 var codes = TempData["codes"];

请注意,TempData中的值仅在您获取其值之前可用。 一旦获取了值,它将被清除。

Tempdata中的值存储为对象,因此您需要将TemtData值强制转换为其相应的类型。

https://msdn.microsoft.com/zh-in/library/dd394711%28v=vs.100%29.aspx

暂无
暂无

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

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