2010-02-08 26 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