有没有办法重用数据注解? [英] Is there a way to reuse data annotations?

查看:110
本文介绍了有没有办法重用数据注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有实现数据域的想法(在属性级别),它在ASP.Net MVC 4视图作为模型类里面的方法?

Is there a way to implement the idea of a data domains (at a property level) inside of a class that is used as a model in a view in ASP.Net MVC 4?

考虑这个code:

public class LoginProfileModel {

    [DisplayName("Login ID")]
    [Required(ErrorMessage = "Login ID is required.")]
    public string LogonID { get; set; }

    [DisplayName("Password")]
    [Required(ErrorMessage = "Password cannot be blank.")]
    [StringLength(20, MinimumLength = 3)]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

下面是一个LoginProfileModel用于ASP.Net MVC 4.采用各种元数据/数据​​注解,这样我可以创建这个code干净的看法:

Here is a LoginProfileModel in for ASP.Net MVC 4. It uses a variety of metadata/data annotations so that I can create a clean view with this code:

@model myWebSite.Areas.People.Models.LoginProfileModel

@using ( Html.BeginForm( "Index" , "Login" ) ) {
    @Html.ValidationSummary()
    @Html.EditorForModel()
    <input type="submit" value="Login" />
}

我用一个登录ID,并在多个视图中的密码的想法,因此,在多个视图模型。我希望能够来定义的属性,一个密码使用,或者可能是密码本身与它的所有数据标注在一个位置,这样我可以重用那些在需要的地方,而不是每次使用时间respecifying他们的定义:

I use the idea of a "Login ID" and a "Password" in more than one view, and therefore, in more than one view model. I want to be able to define the attributes that a Password uses, or possibly the Password itself with all of it's data annotations in a single location so that I can reuse all those definitions where needed, rather than respecifying them every time they are used:

    [DisplayName("Password")]
    [Required(ErrorMessage = "Password cannot be blank.")]
    [StringLength(20, MinimumLength = 3)]
    [DataType(DataType.Password)]
    public string Password { get; set; }

这是可能的某种方式?

Is that possible some way?

推荐答案

下面的属性会影响您查看的验证过程。

The following attributes affect the validation process of your View.

[Required(ErrorMessage = "Password cannot be blank.")]
[StringLength(20, MinimumLength = 3)]

有关验证属性,你可以创建一个这样的类:

For the Validation attributes, you can create a class like this:

public class PasswordRuleAttribute : ValidationAttribute
    {    
        public override bool IsValid(object value)
        {

            if (new RequiredAttribute { ErrorMessage = "Password cannot be blank." }.IsValid(value) && new StringLengthAttribute(20) { MinimumLength=3 }.IsValid(value) )
                return true;

            return false;
        }
    }

您可以按如下方式使用它:

You can use it as follows:

[PasswordRule]
public string Password{get;set;}

这是你提到的是直接从属性类派生的,我不认为有一种方法将它们整合成一个单一属性的另外两个属性。

The other two attributes that you mentioned are Directly derived from the Attribute class, and I don't think there's a way to consolidate them into a single attribute.

我会尽快编辑更新你。

所以,现在我们只剩下:

So now we're left with:

[DisplayName("Password")]
[DataType(DataType.Password)]
[PasswordRule]
public string Password{get;set;}

编辑:

根据这篇文章:复合属性,这是不可能的合并属性。

According to this post: Composite Attribute, it's not possible to merge attributes.

这篇关于有没有办法重用数据注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆