简体   繁体   中英

Can't add ModelBinder attribute to a property of input model

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?

您可能必须像下面这样实现自己的PropertyBinderMVC 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM