I have an application which allows a user to log in to the system. When the user is granted access to the site, I pull a unique identifier from a database that is associated with the user ID and password. I need to pass this unique identifier from controller to controller so any database interaction (creating/editing/deleting records) will only effect records associated with the specific user.
I am using TempData["Message"] to provide text messages from the controller to the view and I am using TempData["GUID"] to pass the unique identifier from controller to controller.
This is what my code looks like in my HomeController:
TempData["Message"] = "";
TempData["GUID"] = user.UserGUID;
TempData.Keep("GUID");
return RedirectToAction("Index");
When I break on the return and view the contents of TempData, I see 2 elements, Message and GUID, and I see the values of each. This is exactly what I wanted.
The RedirectToAction("Index") brings me to the Index action where I perform some database interaction but I make no reference to TempData["GUID"]. I then put up a view.
The view provides the user with a hyperlink which looks like this:
<a class="nav-link text-dark" asp-area="" asp-controller="Boxes" asp-action="Create">Add a Box</a>
When the user clicks the link, the BoxesController is called and the Create action executes. I have a breakpoint on the very first line of this action. When I hit the breakpoint and look at the watch, TempData["Message"] has a value of blank and TempData["GUID"] is no longer part of TempData.
It looks as if TempData["Message"] is maintained between HomeController and BoxesController, but TempData["GUID"] only exists in HomeController.
Any ideas why this might be?
Thank you.