C# ASP.NET MVC如何查找已出错的属性
我希望ModelBinder中出现错误,自动在ModelState中追加错误代码。 因此,我扩展了默认属性以添加属性(关于RequiredAttribute、RangeAttribute等),如: 但我不知道,我怎么知道ModelBinder中哪个属性是错误的C# ASP.NET MVC如何查找已出错的属性,c#,asp.net-mvc,modelbinders,model-validation,C#,Asp.net Mvc,Modelbinders,Model Validation,我希望ModelBinder中出现错误,自动在ModelState中追加错误代码。 因此,我扩展了默认属性以添加属性(关于RequiredAttribute、RangeAttribute等),如: 但我不知道,我怎么知道ModelBinder中哪个属性是错误的 public class TestModelBinder : DefaultModelBinder { protected override void SetProperty(ControllerContext control
public class TestModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor,
object value)
{
//the error was happend in here
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
//in here to find which attribute has been error?
to find attribute and to get ErrorCode then
bindingContext.ModelState[modelStateName+code] = ...
}
}
您可以访问MVC的源代码 以及重写DefaultModelBinder
public class TestModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor,
object value)
{
//the error was happend in here
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
//in here to find which attribute has been error?
to find attribute and to get ErrorCode then
bindingContext.ModelState[modelStateName+code] = ...
}
}