开发者

is Valid never invoke Custom Validation Attribute

public class SomeValidator : ValidationAttribute
{
    public SomeValidator()
        : base("Message")
    {

    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
开发者_JAVA百科        return new ValidationResult("ERROR");
    }

And :

    [SomeValidator]
    public long Something { get; set; }

Why isValid method is never invoked ? (I use ASP MVC 3) Thanks for help!


You must ensure that you have a controller action taking this model as action argument:

public class MyViewModel
{
    [SomeValidator]
    public long Something { get; set; }
}

and then:

public ActionResult SomeAction(SomeModel model)
{
    ...
}

or that you call the UpdateModel/TryUpdateModel method:

public ActionResult SomeAction()
{
    var model = new SomeModel();
    if (TryUpdateModel(model))
    {

    }
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜