如何在一个actionresult中保存临时值对象,并在另一个actionresult中使用它

本文关键字:actionresult 对象 另一个 保存 一个 | 更新日期: 2023-09-27 18:00:31

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

这是我的第一个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; }

任何帮助都会对有很大帮助

如何在一个actionresult中保存临时值对象,并在另一个actionresult中使用它

使用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/en-in/library/dd394711%28v=vs.100%29.aspx