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;
}
}