有没有办法使用多个 System.Web.Mvc.RemoteAttributes?获取“重复的 RemoteAttribute 属性”。

发布于 2024-10-20 20:37:04 字数 739 浏览 1 评论 0原文

我需要对视图模型属性执行两个单独的验证。显然,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-10-27 20:37:04

如果不重写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文