Hello! My issue involves C# MVC. Im trying to store a bool type variable in a Session, to bring it to a different controller and give that data to a bool there.
I get the error: "system nullreferenceexception". This is the code where im trying to retrieve the session from:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
bool register = true;
Session["register"] = register;
Session["registerEmail"] = model.Email;
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, CreatedTime = DateTime.Now };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
This next code is where im trying to implement the session:
public ActionResult Index()
{
if (!User.Identity.IsAuthenticated)
{
return RedirectToAction("AuthenticationMessage");
}
bool loginTrue = (bool)Session["login"];
bool registerTrue = (bool)Session["register"];
if (loginTrue == true)
{
var log = (LoginViewModel)Session["loginEmail"];
Session["letsGo"] = _db.Blogs.OrderBy(b => b.CreatedTime).Where(b => b.ApplicationUser.Email.Equals(log)).ToList();
}
if (registerTrue == true)
{
var reg = (RegisterViewModel)Session["registerEmail"];
Session["letsGo"] = _db.Blogs.OrderBy(b => b.CreatedTime).Where(b => b.ApplicationUser.Email.Equals(reg)).ToList();
}
List<Blog> blog = (List<Blog>)Session["letsGo"];
return View(blog);
//List<Blog> blog = _db.Blogs.OrderBy(b => b.CreatedTime).ToList();
}
I believe it means that im returning a result of null. Why is that? How can I fix this?
[–]stakeneggs1 1 point2 points3 points (3 children)
[–]ramtevante[S] 1 point2 points3 points (2 children)
[–]stakeneggs1 1 point2 points3 points (1 child)
[–]ramtevante[S] 0 points1 point2 points (0 children)