开发者

ASP.NET MVC 1 - Calling the Session object in ViewModel

How can I call the Session in ViewModel?, Reference to "Session [...]" or " HttpContext.Session [..]" not 开发者_高级运维exist in this context


try

HttpContext.Current.Session[..]


The general idea is that you "shouldn't."

Your controller should provide all of the information that the view needs.

However, it may be worthwhile to pass the session (or pieces of it) along with the ViewModel.

The way I handle this is, I have a base class for all of my view models that have access to the controller. They can then directly query the controller for specific objects from the session without ever exposing the session directly to the view.

BaseView.cs

public abstract class BaseView<TModel> : SparkView<TModel> where TModel : ControllerResponse
{
    // Stuff common to all views.
}

ControllerResponse.cs (base model for all views)

public class ControllerResponse
{
    private BaseController controller = null;

    private ControllerResponse() { }

    public ControllerResponse(BaseController controller)
    {
        this.controller = controller;
    }

    // Here, you would place all of the methods that the BaseView should have access to.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜