Csharp/C#教程:有没有办法重用数据注释?分享


有没有办法重用数据注释?

有没有办法在ASP.Net MVC 4中的视图中用作模型的类内部实现数据域(在属性级别)的概念?

考虑以下代码:

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; } } 

这是ASP.Net MVC 4中的LoginProfileModel。它使用各种元数据/数据注释,以便我可以使用以下代码创建一个干净的视图:

 @model myWebSite.Areas.People.Models.LoginProfileModel @using ( Html.BeginForm( "Index" , "Login" ) ) { @Html.ValidationSummary() @Html.EditorForModel()  } 

我在多个视图中使用“登录ID”和“密码”的概念,因此,在多个视图模型中使用。 我希望能够定义密码使用的属性,或者可能是密码本身及其所有数据注释在一个位置,以便我可以在需要时重用所有这些定义,而不是每次使用时重新指定它们:

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

这有可能吗?

以下属性会影响View的validation过程。

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

对于Validation属性,您可以创建一个这样的类:

 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; } } 

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

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

您提到的其他两个属性是直接从Attribute类派生的,我认为没有办法将它们合并为单个属性。

我很快就会通过编辑为您更新。

所以现在我们离开了:

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

编辑:

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

您可以使用伙伴类来执行此操作,该类为视图模型提供元数据。 像这样:

 public partial class LogonMetaData { [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; } } 

然后你的视图模型:

 using System.ComponentModel.DataAnnotations; [MetadataType(typeof(LogonMetaData))] public partial class FirstViewModel { public string LogonID { get; set; } public string Password { get; set; } } using System.ComponentModel.DataAnnotations; [MetadataType(typeof(LogonMetaData))] public partial class SecondViewModel { public string LogonID { get; set; } public string Password { get; set; } } 

注意在类定义中使用partial 。 这是允许这种方法工作的原因。 除了DRY的明显问题之外,一个警告是,我认为元数据类必须与视图模型位于相同的命名空间中,否则它会抱怨。 除此之外,这应该做你想要的。

作为John H的答案的必然结果,您可以使用inheritance并使那些具有“LogonId和密码的概念”的视图模型inheritance自该基本视图模型。 这将解决前一个答案中提到的MetaData问题。

  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; } } public SomeOtherClassThatNeedsLoginInfo : LoginProfileModel{ public string Property {get;set;} } 

现在,在SomeOtherClassThatNeedsLoginInfo中,您可以使用这些属性及其相关的DataAnnotations。

另一个想法是将LoginInfo作为属性传递给其他视图模型。

上述就是C#学习教程:有没有办法重用数据注释?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public SomeOtherClassThatNeedsLoginInfo{ public string Property {get;set;} public LoginProfileModel LoginModel {get;set;} } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1269773.html

(0)
上一篇 2022年11月22日
下一篇 2022年11月22日

精彩推荐