public class SomeValidator : ValidationAttribute
{
public SomeValidator()
: base("Message")
{
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return new ValidationResult("ERROR");
}
并且:
[SomeValidator]
public long Something { get; set; }
为什么永远不会调用isValid方法? (我使用ASP MVC 3)感谢您的帮助!
答案 0 :(得分:1)
您必须确保您有一个控制器操作,将此模型作为操作参数:
public class MyViewModel
{
[SomeValidator]
public long Something { get; set; }
}
然后:
public ActionResult SomeAction(SomeModel model)
{
...
}
或您调用UpdateModel
/ TryUpdateModel
方法:
public ActionResult SomeAction()
{
var model = new SomeModel();
if (TryUpdateModel(model))
{
}
...
}