You could mimic what ViewBag
does.
First, it uses an internal sealed class called DynamicViewDataDictionary
. Basically, I'm just going to make a version of that for TempData. Then, we can use extension methods to make it available in Controller
, WebViewPage
, etc.
public sealed class DynamicTempDataDictionary : DynamicObject
{
private readonly Func<TempDataDictionary> _tempDataThunk;
public DynamicTempDataDictionary(Func<TempDataDictionary> viewDataThunk)
{
_tempDataThunk = viewDataThunk;
}
private TempDataDictionary ViewData
{
get { return _tempDataThunk(); }
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return ViewData.Keys;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = ViewData[binder.Name];
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
ViewData[binder.Name] = value;
return true;
}
}
public static class TempBagExtensions
{
private const string TempBagKey = "__CurrentTempBagDictionary";
public static dynamic TempBag(this ControllerBase controller)
{
return GetCurrentDictionary(controller.ControllerContext);
}
public static dynamic TempBag(this WebViewPage viewPage)
{
return GetCurrentDictionary(viewPage.ViewContext.Controller.ControllerContext);
}
private static DynamicTempDataDictionary GetCurrentDictionary(ControllerContext context)
{
var dictionary = context.HttpContext.Items[TempBagKey] as DynamicTempDataDictionary;
if (dictionary == null)
{
dictionary = new DynamicTempDataDictionary(() => context.Controller.TempData);
context.HttpContext.Items[TempBagKey] = dictionary;
}
return dictionary;
}
}
In your controller:
this.TempBag().SomeValue = "Test";
In your razor view:
@this.TempBag().SomeValue
If you don't think the extension method is clean enough, you could create your own Controller base class, as well as your own base classes for razor views that use nice, concise properties ala ViewBag
.