ASP.Net MVC2 中的常见数据注释

发布于 2024-09-02 14:17:06 字数 1886 浏览 4 评论 0原文

你好,我有一个应该很简单的问题。我有一组使用 System.CompontentModel.DataAnnotations 的验证。我有一些特定于某些视图模型的验证,因此我很乐意将验证代码放在与我的模型相同的文件中(如 MVC2 附带的默认 AccountModels.cs 文件中)。但我有一些适用于多个模型的常见验证(例如有效的电子邮件地址格式)。当我将该验证剪切/粘贴到需要它的第二个模型时,我当然会收到重复定义错误,因为它们位于同一名称空间(projectName.Models)中。因此,我考虑将公共验证删除到命名空间内的单独类中,期望我的所有视图模型都能够从那里访问验证。出乎意料的是,验证不再可访问。我已经验证它们仍然在同一个命名空间中,并且它们都是公共的。我不希望我必须对它们有任何特定的引用(尝试为同一名称空间添加 using 语句,但这并没有解决它,并且通过添加引用对话框,项目无法引用自身(使得那么

知道为什么只是移动到同一命名空间中的另一个文件的公共验证对我的模型不可见吗?

这是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace ProjectName.Models
{
    public class CommonValidations
    {
        [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
        public sealed class EmailFormatValidAttribute : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                if (value != null)
                {
                    var expression = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
                    return Regex.IsMatch(value.ToString(), expression);
                }
                else
                {
                    return false;
                }
            }
        }
    }
}

我想要使用验证的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Growums.Models;

namespace ProjectName.Models
{
    public class PrivacyModel
    {
        [Required(ErrorMessage="Required")]
        [EmailFormatValid(ErrorMessage="Invalid Email")]
        public string Email { get; set; }
    }
}

Howdy, I have what should be a simple question. I have a set of validations that use System.CompontentModel.DataAnnotations . I have some validations that are specific to certain view models, so I'm comfortable with having the validation code in the same file as my models (as in the default AccountModels.cs file that ships with MVC2). But I have some common validations that apply to several models as well (valid email address format for example). When I cut/paste that validation to the second model that needs it, of course I get a duplicate definition error because they're in the same namespace (projectName.Models). So I thought of removing the common validations to a separate class within the namespace, expecting that all of my view models would be able to access the validations from there. Unexpectedly, the validations are no longer accessible. I've verified that they are still in the same namespace, and they are all public. I wouldn't expect that I would have to have any specific reference to them (tried adding using statement for the same namespace, but that didn't resolve it, and via the add references dialog, a project can't reference itself (makes sense).

So any idea why public validations that have simply been moved to another file in the same namespace aren't visible to my models?

CommonValidations.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace ProjectName.Models
{
    public class CommonValidations
    {
        [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
        public sealed class EmailFormatValidAttribute : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                if (value != null)
                {
                    var expression = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
                    return Regex.IsMatch(value.ToString(), expression);
                }
                else
                {
                    return false;
                }
            }
        }
    }
}

And here's the code that I want to use the validation from:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Growums.Models;

namespace ProjectName.Models
{
    public class PrivacyModel
    {
        [Required(ErrorMessage="Required")]
        [EmailFormatValid(ErrorMessage="Invalid Email")]
        public string Email { get; set; }
    }
}

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

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

发布评论

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

评论(1

辞别 2024-09-09 14:17:06

您已将 EmailFormatValidAttribute 声明为 CommonValidations 的子类。因此,您需要像 CommonValidations.EmailFormatValidAttribute 一样引用它。或者将 EmailFormatValidAttribute 类移出 CommonValidations 类。

这应该可行:

[CommonValidations.EmailFormatValid(ErrorMessage="Invalid Email")]
public string Email { get; set; }

顺便说一句,您可以按如下方式简化您的类:

public class EmailFormatValidAttribute : RegularExpressionAttribute
{            
   public EmailFormatValidAttribute() : 
      base(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
   {}
}

另外,请看一下:数据注释扩展。这是一个很棒的 DataAnnotations 库,其中已经包含了最常见的验证。

You have declared EmailFormatValidAttribute as a subclass to CommonValidations. As such you need to reference it like CommonValidations.EmailFormatValidAttribute. Or alternatively move the EmailFormatValidAttribute class out of the CommonValidations class.

This should work:

[CommonValidations.EmailFormatValid(ErrorMessage="Invalid Email")]
public string Email { get; set; }

By the way, you can simplify your class as follows:

public class EmailFormatValidAttribute : RegularExpressionAttribute
{            
   public EmailFormatValidAttribute() : 
      base(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
   {}
}

Also, take a look at this: Data Annotations Extensions. It's a great DataAnnotations library which has already the most common validations included in it.

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