2010-02-08 69 views

回答

4

尝试

HttpContext.Current.Session[..] 
+0

在此上下文中不存在对“HttpContext ...”的引用 – 2010-02-08 16:49:06

+0

对不起,调用“System.Web”/“using System.Web”我可以得到一个会话 – 2010-02-08 17:30:20

+0

抱歉忘了添加完全限定的名称。很高兴听到你的工作。 – 2010-02-08 17:41:03

5

总体思路是你“不应该”。

您的控制器应提供视图所需的所有信息。

但是,将会话(或其片段)与ViewModel一起传递可能是值得的。

我处理这个问题的方式是,我有一个可以访问控制器的所有视图模型的基类。然后,他们可以直接向会话控制器查询会话中的特定对象,而不会将会话直接暴露给视图。

BaseView.cs

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

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. 
} 
+0

+1“的总体思路是,你不应该” T“。 – mxmissile 2010-02-08 16:58:01

+1

+1对于一个答案,在我的情况下,因为这是一个ViewModel对应于“ascx”,更简单(不正确),调用System.Web.HttpContext.Current.Session。谢谢。 – 2010-02-08 17:35:32