data-annotations

DataAnnotations validation from a class

谁说我不能喝 提交于 2019-11-27 03:06:23
问题 I am using DataAnnotations in a project that is a pure C# application, what is the best way to validate my models/documents against the DataAnnotations attributes? 回答1: This is now build into C# 4 var result = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(Vehicle, new ValidationContext(Vehicle, null, null), result); This will also give you the details of the validation. 回答2: Not from me but my friend Steve Sanderson: internal static class

Validate data using DataAnnotations with WPF & Entity Framework?

眉间皱痕 提交于 2019-11-27 02:55:44
Is there any way to validate using DataAnnotations in WPF & Entity Framework? Jeremy Gruenwald You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you're using a "buddy" class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof

Globally localize validation

别说谁变了你拦得住时间么 提交于 2019-11-27 02:33:48
问题 I'm using System.ComponeneModel.DataAnnotations attributes such as Required and StringLength. Is it possible to localize its error messages globally? I know I can do this [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))] But doing this everywhere I use required attribute would be just insane. Also I'd like to avoid stuff like: public class LocalizedRequiredAttribute : RequiredAttribute { public LocalizedRequiredAttribute() : base() {

Mixing Fluent API and DataAnnotations in EF code first

↘锁芯ラ 提交于 2019-11-27 02:11:16
While we mostly use fluent configuration for our code-first POCOs, we have found it useful to use data annotations for things like the table name, PKs, etc. since it makes it easier for non-EF components that don't have a reference to the ObjectContext to interact with these entities. In our experience, it seems that the two configuration styles can be mixed freely, with fluent configuration overriding DataAnnotations. Is this documented anywhere? Is there any risk to doing this mixed configuration? We are currently using EF 4.3.1 I personally haven't ran into any issues with mixing the code

localize default model validation in mvc 2

点点圈 提交于 2019-11-27 01:55:26
问题 [Required] [DisplayName("my date")] public DateTime? DateReg { get; set; } so if the user is going to pass in an invalid datetime value he will get this message "The value '02.07.201022' is not valid for my date." how can I translate/localize this message ? 回答1: Add Messages.resx in App_GlobalResources and in Application_Start in Global.asax : DefaultModelBinder.ResourceClassKey = "Messages"; Then in the Messages.resx file you could define the following string: PropertyValueInvalid: The value

WPF Binding : Use DataAnnotations for ValidationRules

南楼画角 提交于 2019-11-27 01:54:49
问题 I have read a lot of Blog post on WPF Validation and on DataAnnotations . I was wondering if there is a clean way to use DataAnnotations as ValidationRules for my entity. So instead of having this (Source) : <Binding Path="Age" Source="{StaticResource ods}" ... > <Binding.ValidationRules> <c:AgeRangeRule Min="21" Max="130"/> </Binding.ValidationRules> </Binding> Where you must have your public class AgeRangeRule : ValidationRule {...} I want the WPF Binding to go see the Age property and look

What parameters does the stringlength attribute errormessage take?

限于喜欢 提交于 2019-11-27 01:28:43
问题 In the MVC4 template one of the data annotation attributes used is stringlength. For example: [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] What parameters {0}, {1}, {2} (more?) are legal? Edit: To be more specific, I can see from the example and trial and error what the possibilities are, but I would like to see some hard documentation. I can't find anything about this in the StringLengthAttribute documentation. 回答1: The {0} index is

How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?

旧城冷巷雨未停 提交于 2019-11-27 01:26:27
问题 In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor (textarea)? I am using [UIHint("MultilineText")] , but can't find any documentation on how to add attributes for the text area. Desired HTML: <textarea cols="40" rows="10"></textarea> Relevant Part of my MVC 3 Model: [UIHint("MultilineText")] public string Description { get; set; } Relevant Part of my Razor cshtml: <div class="editor-field"> @Html.EditorFor(model => model.Description) </div> What I'm getting in View

Data Annotations with Entity Framework 5.0 (database first)

回眸只為那壹抹淺笑 提交于 2019-11-27 01:24:33
问题 What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach? This is my partial class created by Entity Framework: //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto

How do Data Annotations work?

跟風遠走 提交于 2019-11-27 01:17:42
问题 I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the model state dictionary? How does the failure to set a property on the model because of model validation bubble up to MVC if no exception is thrown? I always assumed that exceptions were thrown every time a property failed and that MVC model