I created a Website(it is poco object) model binder, that checks the sessions: public class WebsitesModelBinder:IModelBinder { private const string websitesSessionName = "SelectedSite";
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.Model != null)
throw new InvalidOperationException("Invalid");
Website site = (Website)controllerContext.HttpContext.Session[websitesSessionName];
if (site == null)
{
site = new Website();
controllerContext.HttpContext.Session[websitesSessionName] = site;
}
return site;
}
#endregion
}
In the global.asax file I registered the model binder for typeof website.
In my controller action, the action gets the website as a parameters and updates it such as:
public ActionResult Websites(Website SelectedSite)
{
var sites = db.Websites.ToList();
if (SelectedSite.ID == 0)
SelectedSite = sites[0];
ViewData["Selectedsite"] = SelectedSite;
return View(sites);
}
However model binder never updates the session Any ideas?