驗證兩個FluentValidation和DataAnnotation接近模型後工作綁定,所以你可以自定義的模型綁定器創建模型:
public class YourBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = (YourModel)base.BindModel(controllerContext, bindingContext);
model.PropA = model.PropA + " catched before validation";
return model;
}
}
在Global.asax中註冊它
protected void Application_Start()
{
ModelBinders.Binders.Add(typeof(YourModel), new YourBinder()); // asssociate model type with binder
}
又通參數您的型號在行動中:
public ActionResult Submit(YourModel model) //YourBinder automatically used
{
if (ModelState.IsValid)
{
//...
}
}