data-annotations

How to add data annotations to partial class?

▼魔方 西西 提交于 2020-01-10 08:57:05
问题 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

DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

泪湿孤枕 提交于 2020-01-08 17:34:40
问题 The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression. Model: [StringLength(100)] [Display(Description = "First Name")] [RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")] public string FirstName { get; set; } Razor View: @Html.TextBoxFor(model => Model.FirstName, new { }) @Html.ValidationMessageFor(model => Model.FirstName) The unobtrusive validation is rendered in view as: <input type=

displayname attribute vs display attribute

独自空忆成欢 提交于 2020-01-08 15:40:30
问题 What is difference between DisplayName attribute and Display attribute in ASP.NET MVC? 回答1: They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property. 回答2: DisplayName sets the DisplayName in the model

displayname attribute vs display attribute

青春壹個敷衍的年華 提交于 2020-01-08 15:39:57
问题 What is difference between DisplayName attribute and Display attribute in ASP.NET MVC? 回答1: They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property. 回答2: DisplayName sets the DisplayName in the model

MVC ModelState IsValid reports true when should be false

你说的曾经没有我的故事 提交于 2020-01-06 10:56:32
问题 I have the following 3 classes (all code now included)... using System.ComponentModel.DataAnnotations; namespace MyWebApp.Models { public class ApplicationUser { [Display(Prompt = "Your Name", Name = "Contact Name"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Please enter a valid contact")] public string ContactName { get; set; } [Display(Name = "Username", Prompt = "Login As"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Your username must be at

MVC ModelState IsValid reports true when should be false

烈酒焚心 提交于 2020-01-06 10:56:11
问题 I have the following 3 classes (all code now included)... using System.ComponentModel.DataAnnotations; namespace MyWebApp.Models { public class ApplicationUser { [Display(Prompt = "Your Name", Name = "Contact Name"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Please enter a valid contact")] public string ContactName { get; set; } [Display(Name = "Username", Prompt = "Login As"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Your username must be at

Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

Deadly 提交于 2020-01-06 04:21:25
问题 Using .Net 3.5 I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property... [Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")] public virtual double Weight{ get; set; } And I have a Validate method in the class that checks validation attributes... protected virtual void Validate() { var type = this.GetType(); foreach (var property in type.GetProperties()) { foreach (ValidationAttribute attribute in property.GetCustomAttributes(typeof(ValidationAttribute),true

Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

流过昼夜 提交于 2020-01-06 04:20:42
问题 Using .Net 3.5 I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property... [Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")] public virtual double Weight{ get; set; } And I have a Validate method in the class that checks validation attributes... protected virtual void Validate() { var type = this.GetType(); foreach (var property in type.GetProperties()) { foreach (ValidationAttribute attribute in property.GetCustomAttributes(typeof(ValidationAttribute),true

MVC2 DataAnnotations validation with inheritance

泄露秘密 提交于 2020-01-04 06:31:08
问题 I have a .NET 2.0 class the properties of which are marked virtual.I need to use the class as a model in a MVC2 application. So, I have created a .NET 3.5 class inheriting from the .NET 2.0 class and added the DataAnnotations attributes to the overriden properties in the new class. A snippet of what I have done is below // .NET 2.0 class public class Customer { private string _firstName = ""; public virtual string FirstName { get { return _firstName; } set { _firstName = value; } } } // .NET

Localization of data annotations in separate class library

吃可爱长大的小学妹 提交于 2020-01-04 03:39:58
问题 We are trying to implement localization for our domain models which are existing in a separate class library project within our solution. However, we are not able to get it working as our models data annotation attributes doesn't get translated at all. Project structure Solution Web project Resource folder (Contains .resx files. Ex. App.en.resx ) Works fine Class library Domain models Resource folder (Contains .resx files. Ex. App.en.resx ) Doesn't work Startup.cs services.AddMvc()