2
我想在ModelBinder中出現錯誤,在ModelState中自動添加錯誤代碼。 所以我不得不擴展了默認的屬性添加屬性(約RequiredAttribute標籤,RangeAttribute等),如:ASP.NET MVC如何查找哪個屬性出錯
//new Attribute
public class TestRequiredAttribute : RequiredAttribute
{
public int ErrorCode { get; set; }
}
但我不kown,我怎麼能kown哪個屬性已被ModelBinder的錯誤?
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] = ...
}
}