data-annotations

Where is the whole list of default error messages for DataAnnotations at MVC 3

冷暖自知 提交于 2019-11-30 06:50:02
Yet another MVC localization question... I'm trying to localize an ASP.Net MVC 3 app using localized Resource files to display texts in the views, as recommended. The problem is, as usual, when trying to localize the default error messages from data annotations. I know you can specify the resource file and key in every Attribute: [Required( ErrorMessageResourceType = typeof(CustomResourceManager), ErrorMessageResourceName = "ResourceKey")] public string Username { get; set; } and even, which is better and preferred, you can override the default message, like this: Default resource for data

Validator.TryValidateObject Not Validating RangeAttribute

*爱你&永不变心* 提交于 2019-11-30 05:36:27
Given the following object, public class Question { [Required] public string QuestionText { get; set; } [Range(1, 5)] public int Difficulty { get; set; } } With the following Validation Code ICollection<ValidationResult> results = new List<ValidationResult>(); Question question = new Question(); ValidationContext ctx = new ValidationContext(question, null, null); Validator.TryValidateObject(question, ctx, results); // results.Length = 1 Why does Range attribute not create a validation error when Required does (the value is 0 obviously)? Ah so it would seem I need to specify

Is it possible to use LabelFor for header row in Index view

拜拜、爱过 提交于 2019-11-30 05:32:28
问题 I am trying to leverage DataAnnotation values in ASP.NET MVC Index view. Interesting, code generator uses field names (eg., BlogPost) as opposed to Html.LabelFor(m => Model.ColumNames["BlogPost"]) or something similar (I made it up). Does it mean, that if the Model is IEnumerable (as in Index view), then it is not possible to get to the Display Name that is specified by DataAnnotation? Hard to believe... My code is currently a mix of MVC 2 and MVC 3, if it makes any difference. 回答1: You

How to add data annotations to partial class?

ε祈祈猫儿з 提交于 2019-11-30 05:14:41
I have an auto generated class with a property on it. I want to add some data annotations to that property in another partial class of the same type. How would I do that? namespace MyApp.BusinessObjects { [DataContract(IsReference = true)] public partial class SomeClass: IObjectWithChangeTracker, INotifyPropertyChanged { [DataMember] public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } private string _name; } } and in another file I have: namespace MyApp.BusinessObjects { public partial class SomeClass { private SomeClass() { }

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

时光总嘲笑我的痴心妄想 提交于 2019-11-30 05:05:16
问题 We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me. As an example I have a model such as: public class HobbyModel { [Required(AllowEmptyStrings =

Different model requirements for POST and PUT

早过忘川 提交于 2019-11-30 05:04:44
问题 Say I have a controller CatController with actions for GET, POST and PUT. They all use the same Cat resource which could look like this: public class CatDto { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public bool IsFriendly {get; set; } } However, the Name and IsFriendly properties should only be required when creating a new cat (POST), but optional when updating it (PUT) to allow updating only a single property. The way I've handled this until now is

MVC3 Validation with ComponentModel.DataAnnotations for UK date format (also using jquery ui datepicker)

我的未来我决定 提交于 2019-11-30 04:55:20
问题 I see there are some similar questions to this, but none solve my issue. I am working on an MVC3 app with Entity Framework 4.3. I have a UK date field that i plan to allow the user to edit using the Jquery UI datepicker (which i got working thanks to this blog). Fortunately for me this blog includes instructions on making the datepicker using UK format, however, the EF validation is still telling me that i need to enter a valid date format. Wierdly it doesn't prevent me from submitting the

Unique constraint with data annotation

a 夏天 提交于 2019-11-30 04:13:37
问题 I'm using the System.ComponentModel.DataAnnotations namespace to validate my domain classes. How can I create a custom attribute to validate the uniqueness of a property regardless of the database (through some interface, for example)? 回答1: This is the solution I came up with for this situation, it simply checks the table for a record with a different id that has the same value for the property being validated. It assumes that you will be using LinqToSQL, and that any table on which this kind

How can I ignore case in a RegularExpression?

流过昼夜 提交于 2019-11-30 03:00:39
问题 I have an asp.net MVC application. There is an entity called File that it has a property called Name. using System.ComponentModel.DataAnnotations; public class File { ... [RegularExpression(@"([^.]+[.](jpg|jpeg|gif|png|wpf|doc|docx|xls|xlsx ..., ErrorMessage = "Invali File Name"] public string Name{ get; set; } ... } There is a RegularExpressionValidator that checks file extensions. Is there a quick way I can tell it to ignore the case of the extension without having to explicitly add the

Is there out-of-the box validator for Enum values in DataAnnotations namespace?

北城以北 提交于 2019-11-30 02:31:36
问题 C# enum values are not limited to only values listed in it's definition and may store any value of it's base type. If base type is not defined than Int32 or simply int is used. I am developing a WCF serivice which needs to be confident that some enum has a value assigned as opposed to the default value for all enums of 0. I start with a unit test to find out whether [Required] would do right job here. using System.ComponentModel.DataAnnotations; using Xunit; public enum MyEnum { // I always