data-annotations

Data Annotation to validate confirm password

不羁岁月 提交于 2020-05-09 18:32:07
问题 My User model has these data annotations to validate input fields: [Required(ErrorMessage = "Username is required")] [StringLength(16, ErrorMessage = "Must be between 3 and 16 characters", MinimumLength = 3)] public string Username { get; set; } [Required(ErrorMessage = "Email is required"] [StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)] [RegularExpression("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", ErrorMessage = "Must be a valid email")]

Entity Framework - [Keyless] Data Annotation Missing

好久不见. 提交于 2020-05-08 05:30:07
问题 According with the Microsoft Documentation Here, I should have access to the Attribute for [Keyless] so I can define my Model has Keyless, so that in my DBContext I could have something like: public DbSet<MyKeylessClass> KeylessModel { get; set; } And use _context.KeylessModel.FromSqlRaw(...) , without having the need to add a PK to it. I have the reference to System.ComponentModel.DataAnnotations and all the Attributes except Keyless are there, what am I missing here? 回答1: You've probably

Validating DataAnnotations on method parameters

核能气质少年 提交于 2020-04-07 07:09:50
问题 How are data annotation attributes to be used with method parameters? I'm expecting to do something like this, but an exception isn't thrown. private string TestValidate([StringLength(5)] string name = "Default: throw exception") { ValidationContext context = new ValidationContext(name); Validator.ValidateObject(name, context); return name; } Alternatively, I know this will work. However, this doesn't use the convenience of the attribute convention. private string TestValidate(string name =

Validating DataAnnotations on method parameters

送分小仙女□ 提交于 2020-04-07 07:09:23
问题 How are data annotation attributes to be used with method parameters? I'm expecting to do something like this, but an exception isn't thrown. private string TestValidate([StringLength(5)] string name = "Default: throw exception") { ValidationContext context = new ValidationContext(name); Validator.ValidateObject(name, context); return name; } Alternatively, I know this will work. However, this doesn't use the convenience of the attribute convention. private string TestValidate(string name =

Validating DataAnnotations on method parameters

百般思念 提交于 2020-04-07 07:09:06
问题 How are data annotation attributes to be used with method parameters? I'm expecting to do something like this, but an exception isn't thrown. private string TestValidate([StringLength(5)] string name = "Default: throw exception") { ValidationContext context = new ValidationContext(name); Validator.ValidateObject(name, context); return name; } Alternatively, I know this will work. However, this doesn't use the convenience of the attribute convention. private string TestValidate(string name =

Generating Data Annotations from Generated Classes

蓝咒 提交于 2020-03-16 07:35:08
问题 I have a linq to sql object or if neccessary Entity Framework object. I want to do MVC 2 Data Annotations for them, but I am endlessly lazy. Is there a way to automatically generate the data annotations a-la [Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")] [MetadataType(typeof(Dinner_Validation))] public partial class Dinner { public bool IsHostedBy(string userName) { return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);

Generating Data Annotations from Generated Classes

◇◆丶佛笑我妖孽 提交于 2020-03-16 07:34:51
问题 I have a linq to sql object or if neccessary Entity Framework object. I want to do MVC 2 Data Annotations for them, but I am endlessly lazy. Is there a way to automatically generate the data annotations a-la [Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")] [MetadataType(typeof(Dinner_Validation))] public partial class Dinner { public bool IsHostedBy(string userName) { return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);

Generating Data Annotations from Generated Classes

你说的曾经没有我的故事 提交于 2020-03-16 07:34:05
问题 I have a linq to sql object or if neccessary Entity Framework object. I want to do MVC 2 Data Annotations for them, but I am endlessly lazy. Is there a way to automatically generate the data annotations a-la [Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")] [MetadataType(typeof(Dinner_Validation))] public partial class Dinner { public bool IsHostedBy(string userName) { return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);

Data Annotations Constructor Injection

独自空忆成欢 提交于 2020-02-22 05:49:47
问题 With ASP.NET MVC, is it possible to use constructor injection with the data annotation attributes (specifically, I'm using validation attributes)? What I would like to be able to do is: public class NoEmailTakenAttribute : ValidationAttribute { public NoEmailTakenAttribute(IService service) { .. } } Is that possible? Thanks. 回答1: You can't use Controller injection from what I see using Reflector, but it does appear possible to use property injection. By creating a class that inherits from

IsValid(object value) has not been implemented by this class

戏子无情 提交于 2020-02-21 11:21:40
问题 I am trying to use asp.net mvc 3 unobstructed javascript with jquery. I am following this Tutorial I am unclear how to do step one. I thought it was just overriding IsValid but I keep getting an error so I must be doing something wrong public class EmailAttribute : ValidationAttribute, IClientValidatable { public override bool IsValid(object value) { return base.IsValid(value); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext