Reputation: 29
I use TempData to store login user role but after the user logs in and presses F5 to reload the page, I run into an error stating that Object reference not set to an instance of an object
I use TempData because my page needs redirection.
The user fills in the login form before submitting it, I don't know how to retrieve the posted data since I need to redirect the user to admin page in case his role is administrator
or just standard
page in case his is normal user
.
private bool IsAdmin(string username)
{
return (Roles.GetRolesForUser(username).ToList().Contains("administrator"));
}
public ActionResult AdminLayout()
{
if(IsAdmin(TempData["LoginUsername"].ToString())) //Error TempDate on Reload
{
return View();
}
else
{
return Index();
}
}
Upvotes: 1
Views: 1691
Reputation: 221
Why don't you store it in a session variable? As the name suggests, TempData is only temporary and session variables will hold the data for longer.
Upvotes: 3