如何为视图模型添加自定义验证方法

发布于 2024-12-05 21:59:22 字数 494 浏览 1 评论 0原文

我尝试为 ViewModel 类添加自定义验证器:

[Serializable]
public class UserViewModel : IValidatableObject 
{
    public IEnumerable<ValidationResult> Validate(ValidationContext context)
    {
        yield return new ValidationResult("Fail this validation");
    }
}

不幸的是,调用 Action 方法时不会触发此操作,例如

    [HttpPost]
    public ActionResult Edit(UserViewModel user)

如何添加自定义验证逻辑? ValidationAttribute 没有提供足够简单的解决方案。我无法找到有关 MVC2 验证机制的明确信息。

I tried to add custom validator for ViewModel class:

[Serializable]
public class UserViewModel : IValidatableObject 
{
    public IEnumerable<ValidationResult> Validate(ValidationContext context)
    {
        yield return new ValidationResult("Fail this validation");
    }
}

Unfortunately this is not triggered when Action method gets called, e.g.

    [HttpPost]
    public ActionResult Edit(UserViewModel user)

How can I add custom validation logic? ValidationAttribute does not provide easy enough solution. I am unable to find clear information on MVC2 validation mechanisms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-12-12 21:59:22

ASP.NET 2.0 不支持 IValidatableObject 。 ASP.NET MVC 3 中添加了对此接口的支持。您可以定义自定义属性:

[AttributeUsage(AttributeTargets.Class)]
public class ValidateUserAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        UserViewModel user = value as UserViewModel;
        // TODO: do some validation

        ErrorMessage = "Fail this validation";
        return false;
    }
}

,然后使用此属性装饰您的视图模型:

[Serializable]
[ValidateUser]
public class UserViewModel
{

}

IValidatableObject is not supported in ASP.NET 2.0. Support for this interface was added in ASP.NET MVC 3. You could define a custom attribute:

[AttributeUsage(AttributeTargets.Class)]
public class ValidateUserAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        UserViewModel user = value as UserViewModel;
        // TODO: do some validation

        ErrorMessage = "Fail this validation";
        return false;
    }
}

and then decorate your view model with this attribute:

[Serializable]
[ValidateUser]
public class UserViewModel
{

}
夏日落 2024-12-12 21:59:22

你的语法看起来是正确的。在您尝试验证模型之前,不会“触发”验证。你的控制器代码应该是这样的

[HttpPost]
public ActionResult Edit(UserViewModel user)
{
    if(ModelState.IsValid)
    {
        // at this point, `user` is valid
    }

    // since you always yield a new ValidationResult, your model shouldn't be valid
    return View(vm);
}

Your syntax looks right. The validation isn't "triggered" until you try to validate your model. Your controller code should look like this

[HttpPost]
public ActionResult Edit(UserViewModel user)
{
    if(ModelState.IsValid)
    {
        // at this point, `user` is valid
    }

    // since you always yield a new ValidationResult, your model shouldn't be valid
    return View(vm);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文