C# 有没有办法重用数据注释?
有没有办法在ASP.NETMVC4中用作视图模型的类内部实现数据域(在属性级别)的思想 考虑以下代码:C# 有没有办法重用数据注释?,c#,asp.net-mvc,viewmodel,data-annotations,code-reuse,C#,Asp.net Mvc,Viewmodel,Data Annotations,Code Reuse,有没有办法在ASP.NETMVC4中用作视图模型的类内部实现数据域(在属性级别)的思想 考虑以下代码: public class LoginProfileModel { [DisplayName("Login ID")] [Required(ErrorMessage = "Login ID is required.")] public string LogonID { get; set; } [DisplayName("Password")] [Req
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; }
}
@model myWebSite.Areas.People.Models.LoginProfileModel
@using ( Html.BeginForm( "Index" , "Login" ) ) {
@Html.ValidationSummary()
@Html.EditorForModel()
<input type="submit" value="Login" />
}
下面是ASP.NETMVC4的登录配置文件模型。它使用了各种元数据/数据注释,因此我可以使用以下代码创建一个干净的视图:
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; }
}
@model myWebSite.Areas.People.Models.LoginProfileModel
@using ( Html.BeginForm( "Index" , "Login" ) ) {
@Html.ValidationSummary()
@Html.EditorForModel()
<input type="submit" value="Login" />
}
有可能吗?以下属性会影响视图的验证过程
[Required(ErrorMessage = "Password cannot be blank.")]
[StringLength(20, MinimumLength = 3)]
对于验证属性,可以创建如下类:
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;}
您提到的其他两个属性直接来自属性
类,我认为没有办法将它们合并到一个属性中
我很快会给你更新一个编辑
现在我们只剩下:
[DisplayName("Password")]
[DataType(DataType.Password)]
[PasswordRule]
public string Password{get;set;}
编辑:
根据这篇文章:,合并属性是不可能的。您可以使用buddy类来实现这一点,它为您的视图模型提供元数据。像这样:
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答案的推论,您可以使用继承,使那些具有“LogonId和密码概念”的视图模型继承自该基本视图模型。这将解决前面答案中提到的元数据问题
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;}
}
现在,在SomeOtherClassThanedsLoginInfo中,您可以使用这些属性及其相关的数据注释
另一个想法是将该登录信息作为属性传递给其他视图模型
public SomeOtherClassThatNeedsLoginInfo{
public string Property {get;set;}
public LoginProfileModel LoginModel {get;set;}
}
在戳了一会儿之后,他认为这是最好的答案。它允许我集中验证逻辑,这最终是我所追求的。分离字段验证的好主意。如果我们能有一个可重用的
[DisplayName]
技术,那就太好了。