1

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"); 
}
ArMaN
  • 2,306
  • 4
  • 33
  • 55
  • Hard to understand what your doing with this. What are you trying to pass? Your method has parameters for `Login` and `otp` but you don't pass any values, only `User` (which wont work if `Users` contains any property which is not a value type). And what is the point of setting `ViewBag` properties if you redirect? –  Dec 17 '14 at 03:44
  • 1
    Show your second view code – Ehsan Sajjad Dec 17 '14 at 04:19
  • Please, try to refine your question as it's really hard to understand. – Mikayil Abdullayev Dec 17 '14 at 05:23
  • As per my understanding of your question, You can store those data on TempData then you can pass between controller actions. also you can pass data between GET and POST methods using @Html.Hidden fields – unique Dec 17 '14 at 06:16

1 Answers1

1

You can use Session or TempData to Persist data between two requests like this simple example: Session:

public ActionResult SampleBook5()
{
    Book book = new Book
    {
        ID = 1,
        BookName = "Sample Book",
        Author = "Sample Author",
        ISBN = "Not available"
    };

    Session["BookData"] = book;
    return RedirectToAction("SampleBook6");
}

public ActionResult SampleBook6()
{
    Book book = Session["BookData"] as Book;

    return View(book);
}

TempData:

public ActionResult SampleBook3()
{
    Book book = new Book
    {
        ID = 1,
        BookName = "Sample Book",
        Author = "Sample Author",
        ISBN = "Not available"
    };

    TempData["BookData"] = book;
    return RedirectToAction("SampleBook4");
}

public ActionResult SampleBook4()
{
    Book book = TempData["BookData"] as Book;

    return View(book);
}

The main difference between using Session and TempData is that we can use session variables to persist the data for a complete user session On the other hand we use TempData to pass some data from one action method to another action method

Aminul
  • 1,738
  • 2
  • 24
  • 42