我创建了一个网站(它是 poco 对象)模型绑定器,用于检查会话: public class WebsitesModelBinder:IModelBinder { private const string websiteSessionName = "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
}
在 global.asax 文件中,我为 typeof 网站注册了模型绑定器。在我的控制器操作中,该操作将网站作为参数获取并对其进行更新,例如: public ActionResult Websites(Website SelectedSite) {
var sites = db.Websites.ToList(); if (SelectedSite.ID == 0) SelectedSite = 站点[0]; ViewData["Selectedsite"] = SelectedSite;
return View(sites);
}
但是模型活页夹永远不会更新会话有什么想法吗?