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

tereško
  • 58,060
  • 25
  • 98
  • 150
Mich
  • 151
  • 1
  • 1
  • 18
  • 1
    You aren't saving your changes. See http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.savechanges(v=vs.113).aspx. – Keith Payne Oct 22 '14 at 09: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 – Mich Oct 22 '14 at 09: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. – Keith Payne Oct 22 '14 at 09:38
  • other controllers / views the error from save changes is this Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. – Mich Oct 22 '14 at 09:38
  • like for instance when, currentUser.CID = user.CID; changes its ok on this but as soon as I move away from this class the variable changes back to the original value – Mich Oct 22 '14 at 09:40
  • Everything goes away after the response is sent to the web browser, so if you aren't persisting your data, then you won't see it again on the next web request. – Keith Payne Oct 22 '14 at 09:40
  • @KeithPayne ok so this leads back to my question, how do i save that to the CID variable? – Mich Oct 22 '14 at 09:42
  • To save it, you have to call `SaveChanges()`. What does the EntityValidationErrors property say about the validation error(s)? – Keith Payne Oct 22 '14 at 09:53
  • how do obtain the EntityValidationErrors I can not find this anywhere to set a brake-point on it – Mich Oct 22 '14 at 10:02
  • actually if I remove the line, ctx.Users.Attach(user); this go's away but the ID isn't updated even when calling SaveChanges() – Mich Oct 22 '14 at 10:05
  • and i am not wanting to save it back to the database – Mich Oct 22 '14 at 10:07
  • 1
    How is `currentUser` declared? Do you understand a new controller is instantiated for each request? Have you heard of session? – CodeCaster Oct 22 '14 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 – Mich Oct 22 '14 at 10:29

1 Answers1

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.

AlanMorton2.0
  • 1,043
  • 2
  • 12
  • 22