2

I have an asp.net mvc4 application in which i have this snippet:

   public bool Logout() {
            try {
                session["user"] = null;
                return true;
            }
            catch {
                return false;
            }
                          }

when i put this code in a controller it's works but if i put it in a model class it didn't. the problem is in the session["user"] = null; .

So how can i manage the session's variables in a model class?

2 Answers 2

2

This functionality should not be in a view model. The model should be used for passing data from controllers to views for displaying, and receiving submitted data from views.

See a question like What is a ViewModel in MVC to get a better explanation.

A logout function should be an action on a controller. Something like:

public ActionResult Logout()
{
    Session["user"] = null;

    // Redirect user to homepage
    return Redirect("/");
}
Sign up to request clarification or add additional context in comments.

Comments

2

In class access by the current context :

HttpContext.Current.Session["user"]....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.