data-annotations

Custom model binding, model state, and data annotations

半世苍凉 提交于 2019-11-28 23:48:55
I have a few questions regarding custom model binding, model state, and data annotations. 1) Is it redundant to do validation in the custom model binder if I have data annotations on my model, because that's what I thought the point of data annotations were. 2) Why is my controller treating the model state as valid even when it's not, mainly I make the Name property null or too short. 3) Is it ok to think of custom model binders as constructor methods, because that's what they remind me of. First here is my model. public class Projects { [Key] [Required] public Guid ProjectGuid { get; set; }

How can I set a RegularExpression data annotation's regular expression argument at runtime?

﹥>﹥吖頭↗ 提交于 2019-11-28 23:16:40
We manage several ASP.NET MVC client web sites, which all use a data annotation like the following to validate customer email addresses (I haven't included the regex here, for readability): [Required(ErrorMessage="Email is required")] [RegularExpression(@"MYREGEX", ErrorMessage = "Email address is not valid")] public string Email { get; set; } What I would like to do is to centralise this regular expression, so that if we make a change to it, all of the sites immediately pick it up and we don't have to manually change it in each one. The problem is that the regex argument of the data

DataAnnotation to compare two properties

时间秒杀一切 提交于 2019-11-28 22:22:19
问题 Is there any way of using data annotations to compare two form field (eg. to confirm an email address) are the same, before allowing the form to be posted? eg. can the regular expression data annotation use the match function to reference another property in a ViewModel? 回答1: Use the CompareAttribute public string EmailAddress {get; set;} [Compare(nameof(EmailAddress), ErrorMessage = "Emails mismatch")] public string VerifiedEmailAddress { get; set; } 回答2: As one possibe option self

Using DataAnnotations on Windows Forms project

别说谁变了你拦得住时间么 提交于 2019-11-28 22:07:35
问题 I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same approach for a Forms project but I'm not sure how to go about it. I have set my attributes but they do not seem to get checked when I click Save. UPDATE: I have used Steve Sanderson's approach which will check for attributes on my class and return a collection of errors like so: try { Business b = new Business(); b.Name = "feds"; b.Description = "DFdsS"; b.CategoryID = 1; b.CountryID = 2; b.EMail = "SSDF"; var

Compare Dates DataAnnotations Validation asp.net mvc

a 夏天 提交于 2019-11-28 22:01:16
Lets say I have a StartDate and an EndDate and I wnt to check if the EndDate is not more then 3 months apart from the Start Date public class DateCompare : ValidationAttribute { public String StartDate { get; set; } public String EndDate { get; set; } //Constructor to take in the property names that are supposed to be checked public DateCompare(String startDate, String endDate) { StartDate = startDate; EndDate = endDate; } public override bool IsValid(object value) { var str = value.ToString(); if (string.IsNullOrEmpty(str)) return true; DateTime theEndDate = DateTime.ParseExact(EndDate, "yyyy

WPF TextBox MaxLength — Is there any way to bind this to the Data Validation Max Length on the bound field?

拥有回忆 提交于 2019-11-28 21:39:23
ViewModel: public class MyViewModel { [Required, StringLength(50)] public String SomeProperty { ... } } XAML: <TextBox Text="{Binding SomeProperty}" MaxLength="50" /> Is there any way to avoid setting the MaxLength of the TextBox to match up my ViewModel (which could change since it is in a different assembly) and have it automatically set the max length based on the StringLength requirement? I used a Behavior to connect the TextBox to its bound property's validation attribute (if any). The behavior looks like this: /// <summary> /// Set the maximum length of a TextBox based on any

Default resource for data annotations in ASP.NET MVC

走远了吗. 提交于 2019-11-28 21:17:50
问题 There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources); then [Required] public string Name { get; set; } someone gimme a light! thanks in advance EDIT My real problem was

ASP.Net MVC 3 ViewModel Data Annotations

自作多情 提交于 2019-11-28 21:17:39
问题 I am developing an ASP.Net MVC 3 Web application with Entity Framework 4.1 and I am getting a bit confused with regards using Data Annotations for form validation. I always return a ViewModel to a View as opposed to passing the actual object as I realise this is poor practice. For example: public class ViewModelTeam { public Team Team { get; set; } } My View might then have something like this @model UI.ViewModels.ViewModelTeam @Html.HiddenFor(model => model.Team.teamID) <div class="editor

Server-side validation of a REQUIRED String Property in MVC2 Entity Framework 4 does not work

烈酒焚心 提交于 2019-11-28 20:43:20
I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working. This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework. The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column. When I try to post to my Create action with an empty string for this Property, I get the follwing error. Exception Details: System.Data.ConstraintException: This property cannot be set to a null value. When I post to the

Custom validation with Data annotations

只谈情不闲聊 提交于 2019-11-28 20:38:16
问题 I'm using data annotations to check data that's being entered, but I'm stuck when it comes to more custom way to validate data. I need to run queries against database to see if stuff exists there or not, and then report back to user if a "custom db-check error" appears, such as "The Companyname already exists" How can I implement such a thing together with dataannotations? I have all the queries done etc using linq and entity framework that comes with 3.5sp1 /M 回答1: Custom attributes that