2009-02-03 84 views

回答

3

你將不得不在TempDataDictionary傳遞給其他類。我這樣做很多,只要其他課程與表達有關(它聽起來像是這樣)就沒有問題。

0

要做到這一點,你需要當前的控制器上下文,否則你不能。

ViewContext.Controller.TempData [ 「不管」] =什麼

1

你做不是需要ControllerContext,你只需要目前的HttpContext

而且你不需要傳遞任何東西,你可以創建一個新的SessionStateTempDataProvider並使用它,因爲這個類的SaveTempData方法唯一的作用是在當前會話的特定鍵上設置一個IDictionary。

(如果您的應用程序未使用任何自定義ITempDataProvider如果你這樣做,你顯然必須依賴於代替。)

SessionStateTempDataProvider是一個非常簡單的類:

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 

using System.Collections.Generic; 
using System.Web.Mvc.Properties; 

namespace System.Web.Mvc 
{ 
    public class SessionStateTempDataProvider : ITempDataProvider 
    { 
     internal const string TempDataSessionStateKey = "__ControllerTempData"; 

     public virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext) 
     { 
      HttpSessionStateBase session = controllerContext.HttpContext.Session; 

      if (session != null) 
      { 
       Dictionary<string, object> tempDataDictionary = session[TempDataSessionStateKey] as Dictionary<string, object>; 

       if (tempDataDictionary != null) 
       { 
        // If we got it from Session, remove it so that no other request gets it 
        session.Remove(TempDataSessionStateKey); 
        return tempDataDictionary; 
       } 
      } 

      return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); 
     } 

     public virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) 
     { 
      if (controllerContext == null) 
      { 
       throw new ArgumentNullException("controllerContext"); 
      } 

      HttpSessionStateBase session = controllerContext.HttpContext.Session; 
      bool isDirty = (values != null && values.Count > 0); 

      if (session == null) 
      { 
       if (isDirty) 
       { 
        throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled); 
       } 
      } 
      else 
      { 
       if (isDirty) 
       { 
        session[TempDataSessionStateKey] = values; 
       } 
       else 
       { 
        // Since the default implementation of Remove() (from SessionStateItemCollection) dirties the 
        // collection, we shouldn't call it unless we really do need to remove the existing key. 
        if (session[TempDataSessionStateKey] != null) 
        { 
         session.Remove(TempDataSessionStateKey); 
        } 
       } 
      } 
     } 
    } 
} 

因此我們可以這樣做:

var httpContext = new HttpContextWrapper(HttpContext.Current); 
var newValues = new Dictionary<string, object> {{"myKey", myValue}}; 
new SessionStateTempDataProvider().SaveTempData(new ControllerContext { HttpContext = httpContext }, newValues); 

請注意,這是覆蓋現有的字典每t ime,所以如果你想順序添加數據,你顯然必須依賴一箇中間容器或者編寫一些額外的邏輯來將現有值與新值合併。

我的情況下,我需要這樣做是爲了錯誤頁面處理,在那裏我做重定向,但我需要有持久性臨時數據的邏輯超出控制器的範圍。

+0

請注意,TempData在MVC框架中有一些默認行爲,例如內容從Session中清除並移動到ControllerContext中的臨時容器。由於TempData實際上只是Session的一個簡單包裝,如果您需要像這樣保存數據,您可能需要考慮編寫自己的會話包裝器,但如果沒有TempData現有行爲的約束,則可能會受益。 – Alex 2014-07-08 04:49:54

1

ControllerBase.TempData物業

System.Web.Mvc.TempDataDictionary

您可以使用公共TempDataDictionary TempData的 {獲得;組; }

使用TempData的

例如像TempData.Clear(); //清除其他類中的TempData