data-annotations

how to put DisplayName on ErrorMessage format

一曲冷凌霜 提交于 2019-11-28 19:19:19
I have something like this: [DisplayName("First Name")] [Required(ErrorMessage="{0} is required.")] [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")] public string Name { get; set; } I want to have the following output: First Name is required. First Name's length should be between 10 and 50. It is working when using ASP.NET MVC2 Error Summary , but when I try to validate it manually, like this: ValidationContext context = new ValidationContext(myModel, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid =

MVC: Override default ValidationMessage

时光怂恿深爱的人放手 提交于 2019-11-28 18:23:23
In the world of MVC I have this view model... public class MyViewModel{ [Required] public string FirstName{ get; set; } } ...and this sort of thing in my view... <%= Html.ValidationSummary("Please correct the errors and try again.") %> <%= Html.TextBox("FirstName") %> <%= Html.ValidationMessage("FirstName", "*") %> My question: If I submit this form without supplying a name, I get the following message "The FirstName field is required" OK. So, I go and change my property to... [DisplayName("First Name")] [Required] public string FirstName{ get; set; } ..and now get "The First Name field is

Asp.Net Mvc Hidden Field from Data Annotations

試著忘記壹切 提交于 2019-11-28 17:21:48
I thought this would be a quick search on google but maybe I'm missing something. Is there a way, using Data Annotations, to set a ViewModel property to create a HiddenInput when the markup get rendered? The only annotations I've found were to hide the property from the view entirely, I still want the property rendered but as a hidden input. This property: [System.Web.Mvc.HiddenInput(DisplayValue = false)] public int Id { get; set; } will be rendered as i.e. <input id="Id" name="Id" type="hidden" value="21" /> when using Html.EditorForModel() or Html.EditorFor(m => m.Id) 来源: https:/

ASP.NET MVC: Is Data Annotation Validation Enough?

99封情书 提交于 2019-11-28 17:03:34
I'm using the Data Annotation validation extensively in ASP.NET MVC 2. This new feature has been a huge time saver, as I'm now able to define both client-side validation and server-side validation in one place. However, while I was doing some detailed testing, I realized that it's quite easy for someone to bypass the server-side validation if I relied on Data Annotation validation alone. For example, if I defined a required field by annotating the property with the [Required] attribute and placed a textbox for that required field in a form, a user could simply remove the textbox from the DOM

localize data annotations default messages ([Required] [StringLength] etc.)

回眸只為那壹抹淺笑 提交于 2019-11-28 16:49:23
问题 if I decorate the properties of my ViewModels with attributes like this: public class Vm { [Required] [StringLength(35)] public string Name {get;set;} } I am going to get english validation messages: "this field is required" "The field Name must be a string with a maximum length of 35" how could I translate them ? 回答1: You could use the ErrorMessageResourceName property: [Required(ErrorMessageResourceName = "SomeResource")] [StringLength(30, ErrorMessageResourceName = "SomeOtherResource")]

Conditionally required property using data annotations

血红的双手。 提交于 2019-11-28 16:38:53
I have a class like this: public class Document { public int DocumentType{get;set;} [Required] public string Name{get;set;} [Required] public string Name2{get;set;} } Now if I put a [Required] data annotation on the Name and Name2 properties, then everything is ok and if Name or Name2 are empty, validation will throw an error. But I want Name field only to be required if DocumentType is equal to 1 and Name2 only required if DocumentType is equal to 2 . public class Document { public int DocumentType{get;set;} [Required(Expression<Func<object, bool>>)] public string Name{get;set;} [Required

The field must be a number. How to change this message to another language?

依然范特西╮ 提交于 2019-11-28 15:49:44
问题 How can I change that messages for all int fields so that instead of saying: The field must be a number in English, it shows: El campo tiene que ser numerico in Spanish. Is there are a way? 回答1: If you happen to be using ASP.NET MVC 4 onwards, check this post: Localizing Default Error Messages in ASP.NET MVC and WebForms Basically you have to add the following piece of code in your Application_Start() method in Global.asax : ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";

ASP.NET MVC Validation form with AngularJS

泄露秘密 提交于 2019-11-28 15:39:27
问题 I'm with a project in MVC 4 and AngularJS (+ twitter bootstrap). I usually use in my MVC projects "jQuery.Validate", "DataAnnotations" and "Razor". Then I enable these keys in my web.config to validate properties of model on the client: <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> For example if I have in my model this: [Required] [Display(Name = "Your name")] public string Name { get; set; } With this Cshtml: @Html.LabelFor(model

Custom Model Validator for Integer value in ASP.NET Core Web API

假如想象 提交于 2019-11-28 13:54:16
I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: public class ValidateIntegerValueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int output; var isInteger = int.TryParse(value.ToString(), out output); if (!isInteger) { return new ValidationResult("Must be a Integer number"); } } return

Unable to set membernames from custom validation attribute in MVC2

 ̄綄美尐妖づ 提交于 2019-11-28 13:27:18
I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied to my viewmodel at the class level as it needs to validate more than one property. I am overriding protected override ValidationResult IsValid(object value, ValidationContext validationContext) and returning: new ValidationResult("Always Fail", new List<string> { "DateOfBirth" }); in all cases where DateOfBirth is one of the properties on my view model. When I run my application, I can see this getting hit. ModelState.IsValid is set to false correctly but when I inspect the ModelState