How to access session in asp.net mvc model

How can I access a session from a model?

I am trying to use:

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

Appears: The name "Session" does not exist in the current context

+6
source share
1 answer

How can I access a session from a model?

You can always do the following pornography:

 HttpContext.Current.Session["Consul"] 

But please, oh, my holy land, please promise me never to commit such a crime.

A model should never know what a session is. Session is a web term and the Model must be completely agnostic. You must pass the information required by the Model from the controller that has access to the session.

So check this out:

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

And when you call this method from the controller, you simply pass the value from the session, because the controller has access to it.

+23
source

Source: https://habr.com/ru/post/919162/


All Articles