目前,我有一個名爲ExistingFileName (下同)自定義驗證屬性,但我已經給它的錯誤消息顯示如何自定義驗證屬性錯誤信息?
protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext)
{
if (value!=null)
{
string fileName = value.ToString();
if (FileExists(fileName))
{
return new ValidationResult("Sorry but there is already an image with this name please rename your image");
}
else
{
return ValidationResult.Success;
}
}
else
{
return new ValidationResult("Please enter a name for your image");
}
}
我已經實現了它,像這樣:
[ExistingFileName]
public string NameOfImage { get; set; }
我確信在設置如下屬性時可以定義錯誤信息:
[ExistingFileName(errormessage="Blah blah blah")]
public string NameOfImage { get; set; }
但我不知道如何?非常感謝任何幫助
很多,謝謝你。 –
你的'IsValid'應該返回一個布爾值,但是你返回一個'ValidationResult'。它是否正確?我無法讓它工作,我無法重寫'IsValid'來返回'ValidationResult'。 – muttley91
感謝您指出,返回類型必須是ValidationResult – Amila