セッションをチェックする Web サイト (poco オブジェクト) モデル バインダーを作成しました。
#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 Web サイトのモデル バインダーを登録しました。私のコントローラー アクションでは、アクションは Web サイトをパラメーターとして取得し、次のように更新します
。if (SelectedSite.ID == 0) SelectedSite = サイト [0]; ViewData["Selectedsite"] = SelectedSite;
return View(sites);
}
ただし、モデル バインダーがセッションを更新することはありません。