无法将 ModelBinder 属性添加到输入模型的属性
我想指定用于输入模型属性的模型绑定器。
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; }
}
但是 ModelBinderAttribute 不能应用于属性。这看起来很愚蠢,因为我可以将它应用于方法参数。我应该怎么做才能解决这个限制?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能必须像这样实现自己的
PropertyBinder
: MVC 属性绑定器You probably have to implement your own
PropertyBinder
like here: MVC Property Binder在想要指定使用哪个模型绑定程序时,您是否打算能够混合和重用模型绑定程序的现有逻辑?如果是这样,您可能可以将您的逻辑合并到自定义活页夹本身中(我猜您的“RadEditorModelBinder”)。这样,您可以使用 1 个模型绑定器,但模型绑定器本身根据传入的属性使用不同的技术。
您认为这对您来说是一个不错的选择吗?如果是这样,请参阅这篇文章进行进一步讨论。
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.