data-annotations

change validate message in data annotation

怎甘沉沦 提交于 2019-11-29 10:46:34
my object has field with data type int. when i put in html form in this textbox letter not number the validator say- The field must be a number. how can i change this messages like this [Required(ErrorMessage = "Введите название")] [DisplayName("Название")] public int age { get; set; } I haven't found a clean way to achieve this using Data Annotations. One way would be to write a custom model binder but this seems like a lot of work to do for such a simple task. Another way to achieve this is to add an App_GlobalResources folder to your ASP.NET application. Add a resource file called Messages

ASP.Net MVC 3 - Client side unobtrusive validation for Editor Templates

那年仲夏 提交于 2019-11-29 09:38:15
问题 I am new to ASP.Net MVC 3, facing some issues while trying to implementing client side unobtrusive validation for a editor template I have created for showing date in a custom way. UI I need to show date in a three texbox UI format as I have put up a EditorTemplate for displaying the date in three parts as @model DateTime? <table class="datetime"> <tr> <td>@Html.TextBox("Day", (Model.HasValue ? Model.Value.ToString("dd") : string.Empty)) </td> <td class="separator">/</td> <td>@Html.TextBox(

Data Annotation attributes are not firing in WCF

这一生的挚爱 提交于 2019-11-29 08:39:06
I am trying to validate the WCF service request using System.ComponentModel.DataAnnotations.dll of Version v4.0.30319. I am using VS2010 with Target Framework v4.0 . Below are my sample request(s). If i invoke the service operation using WcfTestclient the annotations are not firing even if i pass the invalid values ( null / String.Empty / "" ) for Name . Request1 : [MessageContract] public class AddUserRequest { [MessageBodyMember] [Required(ErrorMessage = "Id is required.")] public int Id { get; set; } [MessageBodyMember] [Required(ErrorMessage = "Name is required.")] [StringLength(100,

Property-level validation errors hinder the validation of Class-level validation

拈花ヽ惹草 提交于 2019-11-29 07:12:14
Update after Bounty was awarded A new solution is coming up to this problem. Please refer to ASP.NET MVC 3 Preview 1 here: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx Look in the section Model Validation Improvements , where you will see the solution to my problem. Original Post Referring to my earlier post How to validate two properties with ASP.NET MVC 2 where I asked how I could compare two properties for Model validation. I did find the answer useful, but I was left with an entirely different problem: Problem : If a Property-level

Uppercase attribute that converts the input to uppercase

时光总嘲笑我的痴心妄想 提交于 2019-11-29 06:57:13
I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the model value to be converted to uppercase when it arrived at the server. At the moment I have the following code within the model: [Required] [Display(Name="Account Code")] [StringValidation(RegExValidation.AccountCode, Uppercase=true)] public string Account { get { return _account; } set { if (value != null) _account = value.ToUpper(); } } But what I would really like is this: [Required] [Display(Name="Account Code")] [StringValidation

DataAnnotations and Resources don't play nicely

左心房为你撑大大i 提交于 2019-11-29 06:38:05
I'm using dataannotations in an MVC2 app and am a little discouraged when trying to use RESX file resources for error messages. I've tried the following but keep getting the exception "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" [Required(ErrorMessage = Resources.ErrorMessages.Required)] [Required(ErrorMessageResourceName = Resources.ErrorMessages.Required, ErrorMessageResourceType = typeof(Resources.ErrorMessages)] I keep getting that error message unless I replace ErrorMessageResourceName with "Required"

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

一世执手 提交于 2019-11-29 06:17:34
问题 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

Using ASP.Net MVC Data Annotation outside of MVC

不打扰是莪最后的温柔 提交于 2019-11-29 05:34:30
i was wondering if there is a way to use ASP.Net's Data annotation without the MVC site. My example is that i have a class that once created needs to be validated, or will throw an error. I like the data annotations method, instead of a bunch of if blocks fired by the initaliser. Is there a way to get this to work? I thought it would be something like: Add data annotations Fire a method in the initialiser that calls the MVC validator on the class any ideas? i must admit i havent added the MVC framework to my project as i was hoping i could just use the data annotations class System

DataAnnotations validation from a class

南楼画角 提交于 2019-11-29 02:35:14
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? 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. Not from me but my friend Steve Sanderson: internal static class DataAnnotationsValidationRunner { public static IEnumerable<ErrorInfo> GetErrors(object instance) { return from prop in TypeDescriptor

mvc [DataType(DataType.EmailAddress) no validation

走远了吗. 提交于 2019-11-29 01:03:23
I'm using this code on an email field: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [DataType(DataType.EmailAddress)] does not work (validation does not occur no at a server not on the client side). I am not sure if I should implement myself a Custom Attribute or I can use one included with MVC 3. Could you please suggest me a solution for creating a custom attribute in case I need to. I read also about some additional extensions, example http://nuget.org/packages/DataAnnotationsExtensions.MVC3 Would you suggest it to me?