开发者

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

开发者 https://www.devze.com 2022-12-15 10:07 出处:网络
I want to specify the model binder to use for a property of my input model. public class SendEmailInpu开发者_如何学运维t

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消