1

I have a Model Binder to help deal with a multi-site website. Each StoreObject has a collection of SiteThemes.

public class StoreObject
{
     .......
     public virtual ICollection<SiteTheme> SiteThemes { get; set; }
}

I will then call a repository object that updates the database with the new theme.

public void SaveAndSetSiteTheme(SiteTheme t)
{
     context.SiteThemes.Where(f => f.StoreObjectID == t.StoreObjectID).ToList().ForEach(p => p.Active = false);
     if (t.SiteThemeID == 0)
     {
            t.Active = true;
            context.SiteThemes.Add(t);
     }
     context.SaveChanges();

}

Once that is completed, the Model Binder below has the correct data and store.SiteThemes will have 1 theme. However, when I refresh the page once again, this Model Binder is out of date. The store.SiteThemes collection reverts to the original state and is empty. I have no idea what on earth is going on. Please help or let me know if more information is required.

public class StoreModelBinder : IModelBinder
{
    private const string sessionKey = "Store";

    private IStoreObjectRepository storeRepository;

    public StoreModelBinder()
    {
        storeRepository = new StoreObjectRepository();
    }

    public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {

        Uri url = HttpContext.Current.Request.Url;
        string dom = url.Host;
        StoreObject store = storeRepository.Stores.FirstOrDefault(s => s.MainURL == dom);
        if (store != null)
            HttpContext.Current.Session[sessionKey] = store;

        return store;
    }
}
FAtBalloon
  • 4,500
  • 1
  • 25
  • 33
  • where is context being instantiated? also Check your connect string. Generally when I have this problem it is because I'm not connecting to the database but it doesn't throw an error. just runs along and no record is created/updated. – Brian Jun 21 '12 at 16:51

1 Answers1

2

I managed to solve this problem. Although, not with an ideal solution. I discovered the following:

"The ModelBinders are reused by MVC for multiple requests. This means they have a longer lifecycle than request scope and therefore aren't allowed to depend on objects with the shorter request scope life cycle."

Source: Inject a dependency into a custom model binder and using InRequestScope using Ninject

I simply added a new instantiation of the StoreObjectRepository within my BindModel to get past the life-cycle issue. I view this as a hack and will hopefully update this code in the near future.

public class StoreModelBinder : IModelBinder
{
    private const string sessionKey = "Store";

    private IStoreObjectRepository storeRepository;

    public StoreModelBinder()
    {
        storeRepository = new StoreObjectRepository();
    }

    public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        storeRepository = new StoreObjectRepository();

        Uri url = HttpContext.Current.Request.Url;
        string dom = url.Host;
        StoreObject store = storeRepository.Stores.FirstOrDefault(s => s.MainURL == dom);
        if (store != null)
            HttpContext.Current.Session[sessionKey] = store;

        return store;
    }
}
Community
  • 1
  • 1
FAtBalloon
  • 4,500
  • 1
  • 25
  • 33