我在asp.net MVC中创建了一个自定义validaton属性,但它无效。验证类调用的构造函数,但IsValid
函数没有调用"提交"按钮按下。我的代码是:
public class TestAttribute :ValidationAttribute
{
private new readonly string ErrorMessage;
public TestAttribute(string ErrorMessage)
{
this.ErrorMessage = ErrorMessage;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if(value!=null)
{
return ValidationResult.Success;
}
return new ValidationResult(this.ErrorMessage);
}
}
用法:
[Test("Error")]
public string User_Pwd { get; set; }
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label class="control-label">Password</label>
@Html.TextBoxFor(m => m.User_Pwd, new { @class = "form-control", @placeholder = "User Password", @maxlength = "50", id = "txt_Pwd" })
@Html.ValidationMessageFor(m => m.User_Pwd)
</div>
</div>
这是一个示例代码。这里是Test类调用的构造函数,但IsValid
函数没有调用。