This project is read-only.

Persisting information between two view requests in MVC

May 15, 2014 at 11:49 PM
Hi

I am basically having two views. The first view takes username as input and generates a password and redirects to the second view. I am storing the user input in a model while the user is entering the controls in the first view and passing that model to the second view. How do I retrieve that values in the second view. Please find my code below and let me know if I am making some mistake.

public ActionResult GetOTP(string Login)
{
// string user = model.Login;

if (!System.String.IsNullOrEmpty(Login))
{
int otpass;

bool conversion = Int32.TryParse(Encryption.GetPassword(Login), out otpass);
if (conversion)
{
//Save user credentials.
User userModel = new User();
userModel.Login = Login;
userModel.Otp = otpass;
string otp1 = otpass.ToString();
userModel.OtpCrtDate = DateTime.Now;

ViewBag.user = userModel.Login;
ViewBag.date = userModel.OtpCrtDate;
ViewBag.password = Convert.ToString(otpass);
ViewBag.status = "OTP: " + otpass + " remains active just 30 seconds from now.";

return View("Access",userModel);
// return View("GetOTP", userModel);
}
else
{
ViewData["status"] = "Sorry, an error was found while creating your password. Try again, please.";
return View("GetOtp");
}
}
else return View("GetOtp");
}

public ActionResult Access(User model,string Login, int otp)
{

if (otp == model.Otp && Login == model.Login)
{
TimeSpan timeSub = DateTime.Now - model.OtpCrtDate;

if (timeSub.TotalSeconds < 30.0)
{
//LogIn successful
//Model.Logged = true;
return View("../PrivateArea/Account");
}
else
{
ViewData["status"] = "Sorry but your OTP is very old. Get a new one.";
return View("UserGetOtp");
}

}
return View("Access");
}