简体   繁体   中英

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

hi i want to hold a temporary code eg."codes" in an actionmethod and use it on another actionmethod to compare if the model.code entered in the view textbox is"codes".But i dont want to save it to database

this is my first actionresult method which will hold the value

  [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);
    }

this is the second actionresult

        [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();
    }

this is the resetPassword view where the code is entered.

@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>

}

this is the resetpassword model

 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; }

any assistance will be of great help

Use 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"];

Please note that value in TempData will be available only till you fetch its value. Once fetched the value, it will be cleared.

The values in Tempdata are stored as objects, so you will need to cast the TemtData value to its respective type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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