Tag: 数据 注释

访问T4模板中的自定义属性(VS2012 MVC4)

我正在尝试访问我的自定义属性,如我正在编写的T4控制器模板中附加到我的模型属性的SortableAttribute 。 我已经将List.tt中的类块,程序集和导入复制为样板文件。 首先,我尝试按如下方式转换属性: 然而,这并没有产生积极的结果,因为我的项目命名空间对T4来说是未知的。 为了解决这个问题,我添加了项目的dll并导入了所需的命名空间。 它起初似乎很成功(例如,命名空间导入没有错误),但它仍然没有找到我的属性。 如果我用MyProject.Filters.SortableAttribute替换SortableAttribute ,则错误消息是在MyProject.Filters找不到SortableAttribute 。 为了解决这个问题,我改变了我的代码如下: 我以为我已经中了大奖,但我很快就意识到这个property.GetCustomAttributes(true)返回所有属性但不是我的…… 示例模型: public class MyModel { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [Display(Name = “Full Name”)] [Sortable] public int Name { get; set; } } SortableAttribute实现: using System; namespace MyProject.Filters { [AttributeUsage(AttributeTargets.Property)] public class SortableAttribute : Attribute { public SortableAttribute(string […]

将数据注释应用于MVC中View模型的子属性?

在属性上放置简单的数据注释很棒, public class UnicornViewModel { [Required] public string Name { get; set; } 但是让我说我​​有这样的事情: public class SuperPower { public class Name { get; set; } } public class UnicornViewModel { [Required] public string Name { get; set; } public SuperPower PrimarySuperPower { get; set; } public SuperPower SecondarySuperPower { get; set; } 如何在PrimarySuperPower.Name上应用Required属性,同时为SecondarySuperPower.Name保留可选属性? 最好是1.与客户端validation相关的东西和2.没有任何特殊处理,例如在Action / […]

C#自定义validation唯一属性 – generics类

我正在尝试进行自定义validation[IsUnique]。 检查属性值是否唯一并返回正确的消息。 这是我的代码,但这只适用于指定的类,是否可以通过元数据来获取正确的类? public class ArticleMetaData { [Required(AllowEmptyStrings = false)] [IsUnique(“Name”)] public String Name{ get; set; } } 我的自定义validation: class IsUnique : ValidationAttribute { public IsUnique(string propertyNames) { this.PropertyNames = propertyNames; } public string PropertyNames { get; private set; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var myproperty = validationContext.ObjectType.GetProperty(PropertyNames); var value = […]

与.net中的数据注释相反?

ASP.NET中[Compare(” “)]数据注释的反面/否定是什么? 即:两个属性必须包含不同的值。 public string UserName { get; set; } [Something[“UserName”]] public string Password { get; set; }