1

Update - this question was originally not about session states.

I am trying to change a variable in the user state in MVC 5

// POST: /Account/Registered [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Registered(loggedinViewModel model, ApplicationUser user, string selcteditem) { if (ModelState.IsValid) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new UserDb())); var store = new UserStore<ApplicationUser>(new UserDb()); var currentUser = manager.FindById(User.Identity.GetUserId()); var newCID = clubIDMethod(); var ctx = store.Context as UserDb; ctx.Users.Attach(user); SelectedNewCID(model, newCID); ViewBag.newCID = (model.SelectedGodCID); user.CID = ViewBag.newCID; currentUser.CID = user.CID; } return View(model); } 

I am really unsure what the state is not being saved, the currentUser.CID is being updated but then when I go else where it's still set to the original value

Please Help Thanks

13
  • 1
    You aren't saving your changes. See msdn.microsoft.com/en-us/library/…. Commented Oct 22, 2014 at 9:31
  • ctx.SaveChanges(); fails and also that would save back to the DB yes? set something to be something different, but not save it back to the db Commented Oct 22, 2014 at 9:34
  • Where is the elsewhere that you are referencing in your question? Because your controller method is pretty much the beginning and end of the user code portion of the web request/response life cycle. Commented Oct 22, 2014 at 9:38
  • 1
    How is currentUser declared? Do you understand a new controller is instantiated for each request? Have you heard of session? Commented Oct 22, 2014 at 10:20
  • 1
    currentUser is obtain for the database, and then set in the identityModels.cs, this is the first time I am needing to update it, and yes I understand a new controller is instantiated per request, I could use a session variable but not sure how in MVC 5 :( please help Commented Oct 22, 2014 at 10:29

1 Answer 1

0

you can add a Session on global.aspx with something like this

 protected void Session_Start(Object sender, EventArgs e) { string temp = string.Empty; HttpContext.Current.Session.Add("_SessionItem", temp); } 

and then you need to assign that the CID with something like this

Session["_SessionItem"] = ViewBag.newCID; 

this will hold the string in a session.

you need to then read that in using the session.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, just what I needed Thanks Mike

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.