2

我需要對視圖模型屬性執行兩個單獨的驗證。顯然,每個屬性只能應用一次RemoteAttribute。這可能是一個愚蠢的問題,但有沒有人知道解決這個問題的方法?有沒有辦法使用多個System.Web.Mvc.RemoteAttributes?獲取「重複的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; } 
} 

回答

2

有沒有辦法解決這個(因爲RemoteAttribute不支持每個屬性的多個聲明)而無需重寫MVC如何處理遠程確認。一個Remote屬性應指向執行所有遠程驗證的服務器上的一個方法。您應該在該服務器方法中彙總多個驗證類型。每個屬性不需要多個遠程屬性的原因是性能,因爲每個額外的回調都會產生開銷。

+0

這是不幸的,因爲我需要能夠有兩種驗證的具體錯誤方法... – gabe 2011-03-07 19:41:55

+0

您始終可以有一個方法調用兩種驗證方法。 – marcind 2011-03-07 20:06:00

+3

如何返回不同的錯誤信息? – 2014-12-07 15:14:39

相關問題