我认识到我的问题与绑定无关,而实际上与验证有关:通过保留值意味着当用户重新加载页面时将填充相同的表单字段,我只需要丢弃验证消息,因为它们没有适用的。
为此,我意识到我可以执行模型属性验证,然后使用一些自定义逻辑来删除验证消息。这与我所做的类似:
public class CaseModel {
public void CleanValidation(ModelStateDictionary dict) {
if( this.ClientPresent ) {
dict.Keys.All( k => if( k.StartsWith("Client") dict[k].Errors.Clear() );
}
}
}
(显然我的实际代码更健壮,但你明白了)
CleanValidation 方法由控制器的 action 方法直接调用:
public void Edit(Int64 id, CaseModel model) {
model.CleanValidation( this.ModelState );
}
我可以通过将CleanValidation
方法添加到新接口IComplexModel
并让新的模型绑定器自动调用此方法来整理它,这样控制器就不需要自己调用它了。
更新:
我有这个接口适用于任何需要复杂验证的 ViewModel:
public interface ICustomValidation {
void Validate(ModelStateDictionary dict);
}
在我原来的例子中,CaseModel
现在看起来像这样:
public class CaseClientModel : ICustomValidation {
public Boolean ClientIsNew { get; set; } // bound to a radio-button
public ClientModel ExistingClient { get; set; } // a complex viewmodel used by a partial view
public ClientModel NewClient { get; set; } // ditto
public void Validate(ModelStateDictionary dict) {
// RemoveElementsWithPrefix is an extension method that removes all key/value pairs from a dictionary if the key has the specified prefix.
if( this.ClientIsNew ) dict.RemoveElementsWithPrefix("ExistingClient");
else dict.RemoveElementsWithPrefix("NewClient");
}
}
OnActionExecuting
在我的公共BaseController
类中调用验证逻辑:
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
base.OnActionExecuting(filterContext);
if( filterContext.ActionParameters.ContainsKey("model") ) {
Object model = filterContext.ActionParameters["model"];
ModelStateDictionary modelState = filterContext.Controller.ViewData.ModelState; // ViewData.Model always returns null at this point, so do this to get the ModelState.
ICustomValidation modelValidation = model as ICustomValidation;
if( modelValidation != null ) {
modelValidation.Validate( modelState );
}
}
}