2013-08-28 30 views
5

Mã mà thường được tạo ra cho một ASP.NET MVC 3 thành viên, escpecially tài sản NewPassword của lớp ChangePasswordModel trông gần như thế: Sử dụng tham số cho StringLength thuộc tính

[Required] 
    [StringLength(100, MinimumLength=6)] 
    [DataType(DataType.Password)] 
    [Display("Name = CurrentPassword")] 
    public string NewPassword { get; set; } 

Nhằm để điền một số thông tin với các thông số bên ngoài tôi đang sử dụng RecourceType:
(Trong trường hợp này tôi đang thay đổi OldPassword và điền vào các thuộc tính Display với một số dữ liệu bổ sung từ một nguồn

[Required] 
    [DataType(DataType.Password)] 
    [Display(ResourceType = typeof(Account), Name = "ChangePasswordCurrent")] 
    public string OldPassword { get; set; } 

Quay lại NewPassword. Tôi làm cách nào để thay thế MinimumLenght bằng Membership.MinRequiredPasswordLength?: nỗ lực của tôi:

[Required] 
    [StringLength(100, MinimumLength=Membership.MinRequiredPasswordLength)] 
    [DataType(DataType.Password)] 
    [Display(ResourceType=typeof(Account), Name= "ChangePasswordNew")] 
    public string NewPassword { get; set; } 

này tạo ra các lỗi:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type (http://msdn.microsoft.com/de-de/library/09ze6t76%28v=vs.90%29.aspx)

+0

Như đã đề cập trong Mô tả lỗi, bạn không thể chuyển biến trong Tham số tối thiểu thuộc tính chuỗi dài. NHƯNG, ** [Bạn có thể viết 'val.Length

+0

cảm ơn. Tôi chỉ khoán ngoài hằng số cho lớp GlobalVariables và mọi thứ đều ổn. Đây dường như là cách dễ nhất –

Trả lời

2

Validation thuộc tính phải được biên dịch hằng (như tiểu bang thông báo lỗi của bạn). Bạn có thể tạo riêng cho bạn ValidationAttribute để xử lý độ dài tối thiểu này cho bạn.

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
public sealed class ValidatePasswordLengthAttribute : ValidationAttribute 
{ 

    private const string DefaultErrorMessage = "'{0}' must be at least {1} characters long."; 

    private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength; 
    public ValidatePasswordLengthAttribute() : base(DefaultErrorMessage) 
    { 
    } 

    public override string FormatErrorMessage(string name) 
    { 
     return string.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, _minCharacters); 
    } 

    public override bool IsValid(object value) 
    { 
     var valueAsString = value.ToString(); 
     return (valueAsString != null) && (valueAsString.Length >= _minCharacters); 
    } 
} 

Sau đó, mô hình điểm của bạn có thể trông như thế này (thậm chí bạn có thể nhận được ưa thích hơn và thêm phần chiều dài tối đa của DataAnnotations của bạn trong ValidatePasswordLength thuộc tính để loại bỏ dòng đó)

[Required] 
[StringLength(100)] 
[DataType(DataType.Password)] 
[Display(ResourceType=typeof(Account), Name= "ChangePasswordNew")] 
[ValidatePasswordLength] 
public string NewPassword { get; set; } 
Các vấn đề liên quan