I want to display some data from HttpContext.Current.Cache via a partial view _CacheData.cshtml in my ASP.NET MVC _Layout.cshtml page. I wrote a wrapper ICacheManager so I can inject it into my controllers. ICacheManager also has its own dependency on IFooRepository
public class CacheManager : ICacheManager
{
public CacheManager(IFooRepository repo, System.Web.Caching.Cache cache)
{ ... }
}
I’m using Ninject.MVC3 nuget package so here is my RegisterServices
private static void(IKernel kernel)
{
kernel.Bind<IFooRepository>().To<FooRepository>();
kernel.Bind<ICacheManager>().To<CacheManager>().WithConstructorArgument("cache", x => HttpContext.Current.Cache);
}
This works great for my controllers, but how can I inject a CacheManager into a partial view? There’s no controller for _CacheData.cshtml. I can get a CacheManager in a base controller and put it in ViewBag, but that doesn’t feel right. Suggestions?
Create a child controller action, where you put the data from cache into a view model and render your partial view with
Html.Action().