ModelBinder does not update the session key
I created a Website(it is poco object) model binder, that checks the sessions: public class WebsitesModelBinder:IModelBinder { private 开发者_如何学Goconst 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?
I know this is old and may not be useful anymore, but I ran across this in a search of my own and wanted to answer from what I did. If you remove the if clause that throws an exception, then this should work. You are returning a reference, so this should update appropriately. Hopefully this helps for the future at least
精彩评论