Question

I want there was an error in ModelBinder,automatically append an error code in the ModelState. so I had extends the default attribute to add a property.(about RequiredAttribute,RangeAttribute etc.) like:

//new Attribute
public class TestRequiredAttribute : RequiredAttribute
{
    public int ErrorCode { get; set; }
}

But I do not kown ,How can i kown which attribute has been error in 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] = ...
    }
}
Was it helpful?

Solution

You can to the source of MVC

http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DefaultModelBinder.cs

and to rewrite DefaultModelBinder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top