對於我的用戶版本,我必須確保密碼和重複密碼相同。我找到了「比較」驗證器,但我無法使它工作。Asp.Net MVC 3:在子屬性上比較驗證器?
我的模型如下所示:
public class UserEditionViewModel{
[Compare("User.Password")]
public String RepeatPassword{get;set;}
public User User {get;set;}
public List<Language> AvailableLanguages{get;set;}
public List<Country> AvailableCountries{get;set;}
}
和用戶模式:
public class User{
[Required]
public String Name{get;set;}
//lot of other properties omitted...
[RegularExpression(@"(|.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z]).*)", ErrorMessageResourceType = typeof(LocalizationResources.Views.User.Edition), ErrorMessageResourceName = "InvalidPassword")]
//And I've localization attributes
public String Password{get;set;}
}
在視圖中我只碰到這樣的:
@Html.PasswordFor(m=>m.User.Password)
@Html.PasswordFor(m=>m.RepeatPassword)
但我得到這個錯誤,即使兩個項目匹配:
'Password repeat' and 'User.Password' do not match.
我在進行客戶端驗證時也出現此錯誤。
對我來說最明顯的錯誤是它找不到子屬性。我對嗎?如果是的話,如何避免這種行爲。如果不是,會有什麼問題?