ASP.NETMVC2で次のようなカスタムモデルバインダーを使用しています。
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
BaseContentObject obj = (BaseContentObject)base.BindModel(controllerContext, bindingContext);
if(string.IsNullOrWhiteSpace(obj.Slug))
{
// creating new object
obj.Created = obj.Modified = DateTime.Now;
obj.ModifiedBy = obj.CreatedBy = controllerContext.HttpContext.User.Identity.Name;
// slug is not provided thru UI, derivate it from Title; property setter removes chars that are not allowed
obj.Slug = obj.Title;
ModelStateDictionary modelStateDictionary = bindingContext.ModelState;
modelStateDictionary.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, null));
...
このバインダーからコントローラーアクションに戻ると、アクションのパラメーターとして提供されているビジネスオブジェクトが正しく変更されます(行obj.Created = .... work)。
ただし、ModelStateは更新されません。これを知っているのは、ビジネスオブジェクトのSlugプロパティにRequiredがあり、カスタムモデルバインダーでModelStateDictionaryを変更して、Slugを提供しているにもかかわらず(上記のように)、ModelState.IsValidはまだfalseです。
デバッグセッションのウォッチウィンドウにModelState["Slug"]を配置すると、エラー(1)があると表示されるため、空であるため失敗します。
カスタムモデルバインダーコード内のModelStateを正しく変更するにはどうすればよいですか?