data-annotations

Localizing validation messages at client side by jQuery in ASP.net MVC

一世执手 提交于 2019-12-03 07:54:28
问题 I am using jquery for client side validation together with data annotations. Everything is working fine but I would like to localize a message when a non numeric value is entered in numeric textbox. For server side validation it can be done by setting DefaultModelBinder.ResourceClassKey to resource class name and providing value for PropertyValueInvalid key. However for client side validation asp.net mvc is using it's own resource file with key 'ClientDataTypeModelValidatorProvider

ASP.NET MVC 3 - Data Annoation and Max Length/Size for Textbox Rendering

感情迁移 提交于 2019-12-03 06:46:10
I know on the Razor View file, we can do something like this @Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) However, I am hoping to create a model for the MVC that can be used to create a form with explicitly defined the size and max length of the textboxes. I try [StringLength(n)] on top of the properties of the model, but that seems to only do the validation ratherh set the size of the textbox. Is there anyway that we can define the length of the text field as a data annotation on top of a property of a model? So ultimately, we could just create the whole form

ASP.NET-MVC 2 DataAnnotations StringLength

ⅰ亾dé卋堺 提交于 2019-12-03 06:36:41
问题 Can I use the MVC 2 DataAnnotations to specify a minimum length for a string field? Has anyone done this or have they created custom attributes and if so do you mind sharing the source? 回答1: If you're using asp.net 4.0, you can use the StringLength attribute to specify a minimum length. Eg: [StringLength(50, MinimumLength=1)] public string MyText { get; set; } 回答2: Use a regular expression attribute. These are interpreted on the client side as well. [RegularExpression(Regexes.MinStringLength)

Validation using attributes

拜拜、爱过 提交于 2019-12-03 06:13:50
问题 I have, let's say, this simple class: public class User { [Required(AllowEmptyStrings = false, ErrorMessage="EmailIsRequired"] public string EmailAddress { get; set; } } I know how to use Validator.TryValidateProperty and Validator.TryValidateObject in the System.ComponentModel.DataAnnotations namespace. In order for this to work, you need an actual instance of the object you want to validate. But now, I want to validate a certain value without an instance of the User class, like:

How to specify a min but no max decimal using the range data annotation attribute?

你。 提交于 2019-12-03 06:06:58
问题 I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value. Here's what I have so far...I'm not sure what the correct way to do this is. [Range(typeof(decimal), "0", "??"] public decimal Price { get; set; } 回答1: How about something like this: [Range(0.0, Double.MaxValue)] That should do what you are looking for and you can avoid using strings. 回答2: If you are concerned about the string looking nice you could do this: [Range(0, Double

Understanding ValidationContext in DataAnnotations

喜欢而已 提交于 2019-12-03 04:51:56
I want to utilize Validator.TryValidateValue() but don't understand the mechanics. Say, i have the following: public class User { [Required(AllowEmptyStrings = false)] [StringLength(6)] public string Name { get; set; } } and the method: public void CreateUser(string name) {...} My validation code is: ValidationAttribute[] attrs = bit of reflection here to populate from User class var ctx = new ValidationContext(name, null, null); var errors = new List<ValidationResult>(); bool valid = Validator.TryValidateValue(name, ctx, errors, attrs); It works fine until value of name is null . I'm getting

Allow empty strings for fields marked with PhoneAttribute or UrlAttribute

有些话、适合烂在心里 提交于 2019-12-03 04:27:08
I'm using CodeFirst Entitty framework 5. I have a class representing a user. public class User { [Key] public int UserId { get; set; } [Url] [DataType(DataType.Url)] [Required(AllowEmptyStrings= true)] public string WebSite { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Phone { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Fax { get; set; } } I like the validation mechanics for Phone and Url attributes a lot, but unfortunately validation fails when fields marked with these

How to automatically add placeholder attribute to html input type number in mvc 4?

▼魔方 西西 提交于 2019-12-03 03:07:23
This is a very specific issue. I managed to automatically add the placeholder attribute to html5 email input type by using an editor template called EmailAddress.cshtml , saved in ~/Views/Shared/EditorTemplates/ folder. See the code below: @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark }) It works because i'm using the [DataType(DataType.EmailAddress)] DataAnnotation in my view model. What doesn't works is when I use a int? variable. public class MiageQuotaRequestViewModel { [Required] [DataType

How can I use DisplayName data annotations for column headers in WebGrid?

妖精的绣舞 提交于 2019-12-03 02:12:52
I have a Car class that I'm trying to display in an MVC 3 view using the WebGrid helper. Below are the Car and it's metadata class. Car class: [MetadataType(typeof(CarMetadata))] public partial class Car { // car implementation } Car metadata class: public class CarMetadata { [DisplayName("Car Name")] [StringLength(100, ErrorMessageResourceType = typeof(ValidationText), ErrorMessageResourceName="CarNameDescriptionLength")] [Required] public string CarName { get; set; } } View contents: @model List<Car> ... var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10); grid.Pager

Localizing validation messages at client side by jQuery in ASP.net MVC

江枫思渺然 提交于 2019-12-02 23:04:55
I am using jquery for client side validation together with data annotations. Everything is working fine but I would like to localize a message when a non numeric value is entered in numeric textbox. For server side validation it can be done by setting DefaultModelBinder.ResourceClassKey to resource class name and providing value for PropertyValueInvalid key. However for client side validation asp.net mvc is using it's own resource file with key 'ClientDataTypeModelValidatorProvider_FieldMustBeNumeric'. The only way to solve it that I found is described at Custom Server and Client Side Required