[英]How to use a compared property's Display Name with the Compare attribute?
這是兩個要比較的屬性:
[Required]
[Display(Name = "Current Password")]
public string Current { get; set; }
[Required]
[Compare("New")]
[Display(Name = "New Password")]
public string New { get; set; }
驗證消息如下:
'Confirm Password' and 'New' do not match.
如何獲得此屬性以使用“ New
屬性的“顯示名稱”?
花了一段時間才能弄清楚這一點,但是顯然我使用了錯誤的Compare
屬性。
我最初的聲明是使用System.ComponentModel.DataAnnotations
的Compare
,而正確的聲明是在System.Web.Mvc
定義的。
DataAnnotations.Compare
沒有相同的功能是很愚蠢的,但是再次愚蠢的是,有兩個Compare
屬性開始。
這是正確的實現:
[Required]
[Display(Name = "New Password")]
public string New { get; set; }
[System.Web.Mvc.Compare("New")]
[Display(Name = "Confirm Password")]
public string ConfirmPassword { get; set; }
希望這對其他人有幫助,因為它確實使我沮喪。 這就是生活。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.