如何在ViewModel中调用Session?在此上下文中不存在对"Session [...]"或"HttpContext.Session [..]"的引用
一般的想法是你"不应该".
您的控制器应提供该视图所需的所有信息.
但是,将会话(或其中的部分)与ViewModel一起传递可能是值得的.
我处理这个问题的方法是,我有一个可以访问控制器的所有视图模型的基类.然后,他们可以直接向控制器查询会话中的特定对象,而无需将会话直接暴露给视图.
BaseView.cs
public abstract class BaseView<TModel> : SparkView<TModel> where TModel : ControllerResponse
{
// Stuff common to all views.
}
Run Code Online (Sandbox Code Playgroud)
ControllerResponse.cs(所有视图的基本模型)
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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2762 次 |
| 最近记录: |