data-annotations

Combining DataAnnotations and IDataErrorInfo for WPF

跟風遠走 提交于 2019-12-05 11:26:18
I am writing a WPF application and I want to use Data Annotations to specify things like Required Fields, Range , etc. My ViewModel classes use the regular INotifyPropertyChanged interface and I can validate the entire object easily enough using the C# 4 Validator , but I would also like the fields to highlight red if they do not validate properly. I found this blog post here (http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/28/wpf-data-validation-using-net-data-annotations-part-ii.aspx) that talks about how to write your base view model to implement IDataErrorInfo and simply use

Client-side validation does not work when inherting from RequiredAttribute in ASP.NET MVC 3?

*爱你&永不变心* 提交于 2019-12-05 10:52:42
I created an inherited attribute like this in ASP.NET MVC3: public sealed class RequiredFromResourceAttribute : RequiredAttribute { public RequiredFromResourceAttribute(string errorResourceName, string errorResourceTypeName) { this.ErrorMessageResourceName = errorResourceName; this.ErrorMessageResourceType = Type.GetType(errorResourceTypeName); } } And use it like this: [RequiredFromResource("Title", "Resources.Resource, MyProject.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")] public string Title { get; set; } It didn't work and the MVC ignored it. Then I create a simpler class

Is it possible to inherit data annotations in C#?

十年热恋 提交于 2019-12-05 10:20:59
Can I inherit the "password" data annotation in another class? public class AccountCredentials : AccountEmail { [Required(ErrorMessage = "xxx.")] [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")] public string password { get; set; } } The other class: public class PasswordReset : AccountCredentials { [Required] public string resetToken { get; set; } **["use the same password annotations here"]** public string newPassword { get; set; } } I have to use different models due to API call's, but like to avoid having to maintain two definitions for the same field. Thanks! Addition:

Data annotations on WCF service contracts

青春壹個敷衍的年華 提交于 2019-12-05 08:16:53
I have a WCF service that has a [DataContract] class defined in it. Each of the properties has the [DataMember] attribute and I have added a couple of Data Annotation attributes [Required] and [StringLength] to a couple of the properties. I then consume this service in an asp.net MVC application as a service reference. When I get a list of all the attributes using var attr= from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>() from attribute in prop.Attributes.OfType<ValidationAttribute>() select attribute; I see none of the data annotations have come through. Is this

Dataannotations localization

淺唱寂寞╮ 提交于 2019-12-05 07:56:23
model [MetadataType(typeof(UserMetaData))] public class User { public int Id { get; set; } public string UserName { get; set; } } public class UserMetaData { public int Id { get; set; } [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "UserNameRequired")] [LocalizedDisplayNameAttribute("UserName", NameResourceType = typeof(Resources.ModelValidation))] public string UserName { get; set; } } view @using (Html.BeginForm()) { <div> @Html.LabelFor(x => x.UserName) @Html.TextBoxFor(x => x.UserName) @Html.ValidationMessageFor(x => x.UserName) </div>

changing viewmodel's MetadataType attribute at runtime

北慕城南 提交于 2019-12-05 07:25:44
In Microsoft MVC 3.0,I have a class: public class Product{ public string Title {get;set;} } This class can be represent as Product or as Service , the only difference between them is just the field labels at View time. so I create two classes : public class ProductMetaData { [Display(Name = "Product")] public object Title { get; set; } } and public class ServiceMetaData { [Display(Name = "Service")] public object Title { get; set; } } How can I set these classes at runtime as MetadataType ? ------------------------ EDIT -------------------------- I found we can set/change metadata for a type

How to make entry field to allow numbers only using EF and Data Annotations?

不羁岁月 提交于 2019-12-05 06:34:16
I am trying to figure out if there is a way to make sure that numeric only input is allowed using Data Annotations and Entity Framework. I am using the following code [Required] [DisplayName("Client No")] [Column("client_no", TypeName = "smallint")] public virtual Int16 Number { get; set; } I want this to be displayed using number class. In one place I use the following <input type="number" name="searchClientNo" class="numericOnly" /><br /> but in the entry form I am using @Html.EditorFor(m => m.Number, EditorTemplate.TextBox) where I have a custom made EditorFor with the following code <div

difference between: [ScaffoldColumn (false)] and [Display (AutoGenerateField = false)]

点点圈 提交于 2019-12-05 06:33:19
To render HTML in my edit view, I use the helper @Html.EditorForModel() . My model: [Required(ErrorMessage = "Campo obrigatório")] [Display(Name = "Nome completo")] public string Name { get; set; } [Required(ErrorMessage = "Campo é obrigatório")] [StringLength(100, ErrorMessage = "A {0} deve ter pelo menos {2} characteres.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Senha")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirmar senha")] [Compare("Password", ErrorMessage = "A nova senha e a confirmação da senha não conincidem.")]

what are the differences between [DataType(DataType.EmailAddress)] & [EmailAddress]

烂漫一生 提交于 2019-12-05 06:32:55
I am trying to understand what are the main differecnes between using [DataType(DataType.EmailAddress)] & [EmailAddress] . inside a model class:- public class MYViewModel { [DataType(DataType.EmailAddress)] OR [EmailAddress] public string Email { get; set; } i did a test and the two attributes will do the following:- will prevent users from adding invalud email address will display the value as "EmailTo:..." but i can not find any differences in respect to the functionality , of course if i use html.TextboxFor then the Datatype will not have any effect, while if i use html.EditorFor then the

DataAnnotations or Application Validation block

北战南征 提交于 2019-12-05 05:58:01
Whats the difference between DataAnnotations and Application Validation Block? DataAnnotations is an attribute based model to 'annotate' your data and it is in the .NET framework itself. Its most obvious use is for validation, as ASP.NET MVC does for instance. Validation Application Block itself is a validation framework, created by the Microsoft P&P team, but it is not part of the .NET framework. It also contains attributes to 'annotate' your data and in its newest version (5.0) the attributes inherit from DataAnnotations, making it interchangeable with DataAnnotations to some extent.