有没有办法使用多个 System.Web.Mvc.RemoteAttributes?获取“重复的 RemoteAttribute 属性”。
我需要对视图模型属性执行两个单独的验证。显然,RemoteAttribute 每个属性只能应用一次。这可能是一个愚蠢的问题,但是有人知道解决这个问题的方法吗?
public class ForgotPasswordModel
{
// Getting compiler error "Duplicate RemoteAttribute attribute"
[Remote("CanFindEmail", "Account", ErrorMessageResourceName = "EmailNotFound", ErrorMessageResourceType = typeof(ValidationMessages))]
[Remote("IsAccountVerified", "Account", ErrorMessageResourceName = "AccountByEmailNotVerified", ErrorMessageResourceType = typeof(ValidationMessages))]
[Required(ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "PropertyRequired")]
[Display(ResourceType = typeof(Resx), Name = "PersonEmailAddress")]
public string Email { get; set; }
}
I need to perform two separate validations on a view model property. Apparently, RemoteAttribute can only be applied once per property. This is probably a stupid question, but does anyone know a way around this?
public class ForgotPasswordModel
{
// Getting compiler error "Duplicate RemoteAttribute attribute"
[Remote("CanFindEmail", "Account", ErrorMessageResourceName = "EmailNotFound", ErrorMessageResourceType = typeof(ValidationMessages))]
[Remote("IsAccountVerified", "Account", ErrorMessageResourceName = "AccountByEmailNotVerified", ErrorMessageResourceType = typeof(ValidationMessages))]
[Required(ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "PropertyRequired")]
[Display(ResourceType = typeof(Resx), Name = "PersonEmailAddress")]
public string Email { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不重写 MVC 处理远程验证的方式,就无法解决这个问题(因为
RemoteAttribute
不支持每个属性多个声明)。单个远程属性应指向服务器上执行所有远程验证的方法。您应该在该服务器方法中聚合多种验证类型。您不希望每个属性有多个远程属性的原因是性能,因为每个额外的回调都会产生开销。There is no way around this (since
RemoteAttribute
does not support multiple declarations per property) without rewriting how MVC handles remote validation. A single Remote attribute should point at a method on the server that performs all remote validation. You should aggregate multiple validation types in that server method. The reason why you don't want multiple remote attributes per property is performance, as every additional callback would have overhead.