0

I am using TempData to pass the guid between two action methods within the same controller, but I am getting null in the second method where I am calling it.I am not able to figure out why I am getting a null value.
Here is what I could do so far:

[HttpPost]
public ActionResult ForgotPassword(ForgotPasswordModel model)
{
     var uniqueIdForUser = db.Database.SqlQuery<RequestResetPasswordModel>("spResetPassword @param1", new SqlParameter("param1", user.ADMIN_USERNAME)).OrderBy(m => m.UserId).FirstOrDefault();
     TempData["uniqueIdForUser"] = uniqueIdForUser.Id;
}

[HttpPost]
public ActionResult ResetPassword(ResetPasswordModel model)
{
     Guid uniqueid = (Guid)TempData["uniqueIdForUser"];
     return View();
}
Sumedha Vangury
  • 643
  • 2
  • 17
  • 43

2 Answers2

0

You should either use Session["uniqueIdForUser"] for this, or even store the value with the user object somewhere in the database. The time between requesting the password reset and actually doing it could be long.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • I have tried with session as well but it still gives me a null reference exception. I have already stored the value in the db table, but I want to compare the value in the session object and the db value, and accordingly update a record in the database – Sumedha Vangury Apr 29 '16 at 07:29
  • On which line (exactly!) does the NullReferenceException occur? If needed, add/edit your code in the question to show this. – Peter B Apr 29 '16 at 07:38
  • its already there in the question, in the ResetPassword method when I am calling the session – Sumedha Vangury Apr 29 '16 at 07:40
  • That means either it was not put into the session, or a `null` value was stored, or the session was cleared. Note that during development, stopping and restarting the application will clear all sessions. Also double check your session keys; always best to use a Const instead of a string literal, just to rule that out. – Peter B Apr 29 '16 at 08:09
  • I am using session the same way like I am using temp data. Storing the session like this: `Session["uniqueIdForUser"] = uniqueIdForUser.Id;` and retrieving it using: ` Guid uniqueid = (Guid)Session["uniqueIdForUser"];` Is there anything else I need to do – Sumedha Vangury Apr 29 '16 at 10:06
0

I know, I am late here. Maybe you got some solution but for those who are in this problem. Maybe

new GUID(Session["something"]);

will help