9

How can I access to the session from the Model?

I try to use:

public IQueryable<EstudentsViewModel> GetEstudentsProjected(decimal? Code)
        {
           .
           .   
           .
           decimal id;
           id = (decimal)Session["Consul"];
           .
           .
           .
        }

Appear: The name 'Session' does not exist in the current context

1
  • What are you trying to accomplish here? Why are you storing this value in session to begin with? I think if you provide answers to those questions, you'll be given a much better alternative that will keep you from having to do this. Commented Jun 27, 2012 at 17:51

1 Answer 1

29

How can I access to the session from the Model?

You could always perform the following pornography:

HttpContext.Current.Session["Consul"]

But please, oh my holy Earth please, promise me to never perform such a crime.

A Model should never know what a Session is. A Session is a web term and the Model should be completely agnostic of it. You should pass the information that the Model requires from the Controller which has access to the Session.

So check this out:

public IQueryable<EstudentsViewModel> GetEstudentsProjected(decimal? Code, decimal id)
{
    ...
}

And when invoking this method from the controller you will simply pass the value from the Session because the Controller has access to it.

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

1 Comment

I know this is very old post, but can't we pass ISessionManager as a parameter? ISessionManager is just a wrapper interface around HttpContext.Current.Session object.

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.