I want to specify the model binder to use for a property of my input model.
public class SendEmailInpu开发者_如何学运维t
{
[Required, EmailAddress]
public string From { get; set; }
[Required]
public string To { get; set; }
[Required]
public string Subject { get; set; }
[Required, ModelBinder(typeof(RadEditorModelBinder))]
public string Body { get; set; }
}
However the ModelBinderAttribute cannot be applied to properties. This seems stupid since I can apply it to method parameters. What should I do to work around this limitation?
You probably have to implement your own PropertyBinder
like here: MVC Property Binder
In wanting to specify which model binder to use, is your intention to be able to mix and re-use exising logic of the model binders? If so, you can probably combine your logic in the custom binder itself (i'm guessing your "RadEditorModelBinder"). This way, you use 1 model binder, but the model binder itself uses different techniques based on the incoming properties.
What do you think, would that be a good alternative for you? If so, see this post for further discussion.
精彩评论