ASP.Net MVC 2 类级别和属性级别的自定义属性可以同时处于活动状态吗?

发布于 2024-12-10 18:07:29 字数 1672 浏览 0 评论 0原文

我正在开发 ASP.NET MVC 2 Web 应用程序。 我的模型具有 3 个属性:

[IsCityInCountry("CountryID", "CityID"]
public class UserInfo
{
    [Required]
    public int UserID { get; set; }

    [Required]
    public int CountryID { get; set; }

    [Required]
    public int CityID { get; set; }
}

我有一个“必需”属性属性,以及类级别的一个属性:

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class IsCityInCountry : ValidationAttribute
{
    public IsCityInCountry(string countryIDProperty, string cityIDProperty)
    {
        CountryIDProperty = countryIDProperty;
        CityIDProperty = cityIDProperty;
    }
    public string CountryIDProperty { get; set; }
    public string CityIDProperty { get; set; }

    public override bool IsValid(object value)
    {
        var properties = TypeDescriptor.GetProperties(value);

        var countryID = properties.Find(CountryIDProperty, true).GetValue(value);
        var cityID = properties.Find(CityIDProperty , true).GetValue(value);

        int countryIDInt;
        int.TryParse(countryID.ToString(), out countryIDInt);

        int cityIDInt;
        int.TryParse(cityID.ToString(), out cityIDInt);

        if (CountryBusiness.IsCityInCountry(countryIDInt, cityIDInt))
        {
            return true;
        }

        return false;
    }
}

当我在视图上发布表单并且未输入 CountryID 时,在 ModelState 字典中存在有关该问题的错误。其他属性被忽略(“IsCityInCountry”)。当我选择不在所选国家/地区的 CountryID 和 CityID 时,我会收到相应的验证消息,并且 ModelState 有另一个键(即“”)。我知道优势有属性属性,然后有类属性。我的问题;有没有办法同时获取所有验证消息,无论涉及哪种属性(类或属性属性)?提前致谢。

I am working on asp.net mvc 2 web application.
I have model with 3 properties:

[IsCityInCountry("CountryID", "CityID"]
public class UserInfo
{
    [Required]
    public int UserID { get; set; }

    [Required]
    public int CountryID { get; set; }

    [Required]
    public int CityID { get; set; }
}

I have one "required" property attribute, and one attribute on class level :

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class IsCityInCountry : ValidationAttribute
{
    public IsCityInCountry(string countryIDProperty, string cityIDProperty)
    {
        CountryIDProperty = countryIDProperty;
        CityIDProperty = cityIDProperty;
    }
    public string CountryIDProperty { get; set; }
    public string CityIDProperty { get; set; }

    public override bool IsValid(object value)
    {
        var properties = TypeDescriptor.GetProperties(value);

        var countryID = properties.Find(CountryIDProperty, true).GetValue(value);
        var cityID = properties.Find(CityIDProperty , true).GetValue(value);

        int countryIDInt;
        int.TryParse(countryID.ToString(), out countryIDInt);

        int cityIDInt;
        int.TryParse(cityID.ToString(), out cityIDInt);

        if (CountryBusiness.IsCityInCountry(countryIDInt, cityIDInt))
        {
            return true;
        }

        return false;
    }
}

When I post the form on my view, and CountryID is not entered, in ModelState dictionary there's an error about that issue. Other attribute is ignored ("IsCityInCountry"). When I choose CountryID and CityID, which is not in selected country, I get appropriate validation message about that, and ModelState has another key (which is ""). I understand that advantage have property attributes and then class attributes. My question; is there any way to get all validation messages at the same time, no matter what kind of attributes are involved (class or property attributes)? Thanks in advance.

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

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

发布评论

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

评论(1

一身骄傲 2024-12-17 18:07:30

如果存在属性级别验证错误,ASP.NET MVC 将不会执行类级别验证。 Brad Wilson 在他的 博客文章

今天早些时候,我们对 MVC 2 进行了更改,将
从输入验证到模型验证的验证系统。

这意味着我们将始终在一个
对象,如果该对象在执行期间至少绑定了一个值
模型绑定。我们首先运行属性级验证器,如果全部
如果成功,我们将运行模型级验证器。

如果您想在 ASP 中执行一些更高级的验证,我建议您继续查看 FluentValidation.NET .NET MVC 应用程序。声明式验证根本不适合高级验证场景。

ASP.NET MVC won't perform class level validation if there are property level validation errors. Brad Wilson explains this in his blog post:

Earlier today, we committed a change to MVC 2 that converted the
validation system from Input Validation to Model Validation.

What this means is that we will always run all validators on an
object, if that object had at least one value bound into it during
model binding. We run the property-level validators first, and if all
of those succeed, we'll run the model-level validators.

I would recommend you to go ahead and checkout FluentValidation.NET if you want to perform some more advanced validation in an ASP.NET MVC application. Declarative validation simply doesn't fit the bill in advanced validation scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文