data-annotations

Using Data Annotations Validation Manually and Object Graphs

牧云@^-^@ 提交于 2019-12-10 10:19:22
问题 Let's assume that I have two simple classes: public class CustomerDetails { [Required] public string Address { get; set; } } public class Customer { public Customer() { Details = new CustomerDetails(); } [Required] public string Name { get; set; } public CustomerDetails Details { get; private set; } } When I try to manually validate Customer class in a Console application in this way: var customer = new Customer() { Name = "Conrad" }; var context = new ValidationContext(customer, null, null);

Is there any way to stop DataAnnotation validation after the first failure?

半世苍凉 提交于 2019-12-10 09:45:32
问题 In my ViewModels I use several DataAnnotations to validate the form data, there are usually 2-3 annotations per field. For example a field for an email address might look like this: [Required(ErrorMessage = "Please enter an email address.")] [Email(ErrorMessage = "That is not a valid email address.")] // Custom public string Email { get; set; } Now if someone were to submit the form, both errors would show up in the validation summary. Is there any easy way to specify an order to run the

About Enum and DataAnnotation

旧街凉风 提交于 2019-12-10 03:53:56
问题 I have this Enum (Notebook.cs): public enum Notebook : byte { [Display(Name = "Notebook HP")] NotebookHP, [Display(Name = "Notebook Dell")] NotebookDell } Also this property in my class (TIDepartment.cs): public Notebook Notebook { get; set; } It's working perfectly, I just have one "problem": I created an EnumDDLFor and it's showing the name I setted in DisplayAttribute, with spaces, but the object doesn't receive that name in DisplayAttribute, receives the Enum name (what is correct), so my

separate numbers by comma with asp.net mvc

浪尽此生 提交于 2019-12-10 03:19:18
问题 I am working on an MVC2 appication. I use data annotations to validate data (both client side and server side). I have several fields in my model that only allows decimal values. As soon as a user types a decimal values I want it to be converted to comma seperated more readable format. For instance 1200 should be formatted to 1,200 while 500 should stay as it is. Here is my model: public virtual GrossFee? Fee { get; set; } And here is how it is on the view: %: Html.TextBoxFor(model => model

ASP.NET MVC ValidateInput(false) stops working with xVal and [RegularExpression] DataAnnotation

白昼怎懂夜的黑 提交于 2019-12-10 00:44:54
问题 I would like to intercept the "<" character in the form field by a regex validator. I will describe the problem in 3 steps: Step 1 : When I try to submit a form with a field containing the "<" character, I get the "Potentially dangerous request..." - as expected in ASP.NET. Step 2 : To avoid ASP.NET's RequestValidation, I decorate my Update method in the controller with "[ValidateInput(false)]". It works as expected - now I can post "<" character without error. Step 3 : I use xVal with

custom unobtrusive date validators for dates

好久不见. 提交于 2019-12-09 22:32:48
问题 Maybe it's just the way my mind works, but I have a very hard time understanding how you're supposed to do custom unobtrusive validators. The C# part is easy enough, but the jqueryui adapters are where i get lost. I've been trying to make a validator that requires a date to be a certain amount of time in the past. I use this for age validation, to make sure someone has entered a date that is 18 years in the past. I finally decided to just make it a remote validator, that way the validation

“Display Name” Data Annotation for class using c#

邮差的信 提交于 2019-12-09 12:24:41
问题 I have a class with [Display(Name ="name")] set in the properties, and [Table("tableName"] in the top of the class. Now I'm using reflection to get some information of this class and I'm wondering if somehow I can add a [Display(Name ="name")] to the class itself. It will be something like [Table("MyObjectTable")] [Display(Name ="My Class Name")] <-------------- New Annotation public class MyObject { [Required] public int Id { get; set; } [Display(Name="My Property Name")] public string

MVC 4 - DataAnnotations - Validation for Type

血红的双手。 提交于 2019-12-09 09:23:30
问题 I have the following code working [Required(ErrorMessage = "Price is required.")] [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price xx.xx")] public decimal? productPrice { get; set; } When the page is submitted with Price = EMPTY Field error message is "Price is required.". Price = over 9999 error message is "Price xx.xx". However, when I type 'aaaa' the error message is "The field productPrice must be a number." How can I change the message if type in not correct? Like : "Price must

DataType vs UiHint

混江龙づ霸主 提交于 2019-12-09 08:07:18
问题 I have been using mvc2 for a while now, and when i need to set the template i use the DataType Attribute [DataType("DropDown")] public int Field { get; set; } I see others using UiHint to achieve the same results [UiHint("DropDown")] public int Field { get; set; } What is the difference between using these two attributes? Which attribute should I be normally using, or are they for different tasks? 回答1: DataType is generally used to make it known that this is a very specific version of a

ASP.NET MVC with jQuery Validation - messages customization and localization

六眼飞鱼酱① 提交于 2019-12-09 07:53:41
问题 I have ASP.NET MVC (4) project which localization is supported by the framework. Should I change browser settings to another language, framework automatically picks up the right resource file. However, because I am using knockoutjs I fall back to jQuery validation at those views. Unfortunately there's no automatic support for localization there. My question is - what are the best practices and ways to customize and localize jQuery validation messages so they will be picked automatically