3

I want to specify the model binder to use for a property of my input model.

public class SendEmailInput
{
    [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?

Andrew Davey
  • 5,441
  • 3
  • 43
  • 57
  • damn it! I went and reorganized things expecting to be able to do this (I have a model that is sometimes bound with a complex modelbinder and sometimes with JSON) – Simon_Weaver Jan 12 '13 at 23:51

2 Answers2

1

You probably have to implement your own PropertyBinder like here: MVC Property Binder

rafek
  • 5,464
  • 13
  • 58
  • 72
0

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.

Community
  • 1
  • 1
Chris Melinn
  • 2,046
  • 1
  • 17
  • 18