开发者

ViewData set in an ActionFilter is coming out null on Master Page MVC3

My Original Issue here:

Help! I've got repository access in my MVC master page! Refac开发者_运维知识库tor time!

What originated as a re factoring job to improve performance has revealed new issues. If I should keep this in my original question - feel free to delete this one, and rename the old one to something more relevant. I'm using an ActionFilter applied to a base controller to load some user-customized settings. My issue is that I save these settings in the filterContext.Controller.ViewData object but its coming through as null on my master page. Here is the relevant code:

Action Filter

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class StoreSettingsActionFilter : ActionFilterAttribute
{
    private readonly IResellerRepository _resellerRepository;
    private readonly IStoreSettingsRepository _storeSettingsRepository;

    public StoreSettingsActionFilter(
        IResellerRepository resellerRepository,
        IStoreSettingsRepository storeSettingsRepository
    )
    {
        _resellerRepository = resellerRepository;
        _storeSettingsRepository = storeSettingsRepository;
    }

    public StoreSettingsActionFilter()
        : this(new ResellerRepository(), new StoreSettingsRepository())
    {

    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);

        var settingsViewModel = new StoreSettingsViewModel();
        settingsViewModel.ThemeLocation = "~/Content/jquery-ui-1.8.9.custom.css";
        var user = filterContext.HttpContext.User;
        if (!user.Identity.IsAuthenticated || !user.IsInRole("Reseller"))
        {
            filterContext.Controller.ViewData["storeSettings"] = settingsViewModel;
            return;
        }

        var session = filterContext.HttpContext.Session;
        var reseller = session["reseller"] as Reseller;
        if (reseller == null)
        {
            reseller = _resellerRepository.GetResellerByUsername(user.Identity.Name);
            session["reseller"] = reseller;
        }

        if (reseller.StoreSettingsID != null && reseller.StoreSetting.Theme != null)
        {
            var storeSettings = session["storeSettings"] as StoreSettings;
            if (storeSettings == null)
            {
                storeSettings = _storeSettingsRepository.GetStoreSettings((int)reseller.StoreSettingsID);
                session["storeSettings"] = storeSettings;
            }
            // Using AutoMapper to convert between the model and the view model
            //settingsViewModel = Mapper.Map<StoreSettings, StoreSettingsViewModel>(storeSettings);

            settingsViewModel.ThemeLocation = storeSettings.Theme.StylesheetLocation;
            settingsViewModel.Address1 = storeSettings.Address1;
            settingsViewModel.Address2 = storeSettings.Address2;
            settingsViewModel.City = storeSettings.City;
            settingsViewModel.FooterImage = storeSettings.Image.FileName;
            settingsViewModel.HeaderImage = storeSettings.Image1.FileName;
            settingsViewModel.Phone = storeSettings.Phone;
            settingsViewModel.PostalCode = storeSettings.PostalCode;
            settingsViewModel.ProvinceCode = storeSettings.Province.Abbreviation;
            settingsViewModel.StoreName = storeSettings.StoreName;
        }
        filterContext.Controller.ViewData["storeSettings"] = settingsViewModel;
    }
}

Base Controller

[StoreSettingsActionFilter]
public abstract class BaseController : Controller
{

}

HomeController snippet

public class HomeController : BaseController
{
    //
    // GET: /Store/Home/
    public ActionResult Index()
    {
        IProductRepository productRepository = new ProductRepository();
        var products = new List<Product>();

        IResellerRepository resellerRepository = new ResellerRepository();
        var reseller = resellerRepository.GetResellerByUsername(User.Identity.Name);

        IProductCategoryRepository categoryRepository = new ProductCategoryRepository();
        var categories = categoryRepository.GetProductCategoriesFromStoreSettings(reseller.StoreSetting.Categories);

        foreach(var category in categories)
        {
           products.AddRange(productRepository.GetProductsByCategory(category.CategoryId));
        }

        var viewModel = new StoreViewModel()
                            {
                                Products = products.ToList()
                            };

        return View(viewModel);
    }
}

Master Page snippet

<head id="Head1">
<% 
var storeSettings = (StoreSettingsViewModel)ViewData["storeSettings"]; // This is always ending up null.
%>
<title>
     <%: string.IsNullOrEmpty(storeSettings.StoreName) ? "My Store Name" : storeSettings.StoreName%>
</title>

I'm really stuck here, your help is greatly appreciated!


Try overriding OnActionExecuted instead of OnResultExecuting in the StoreSettingsActionFilter filter:

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜