model-validation

How to validate using annotations in a nested class

巧了我就是萌 提交于 2019-12-25 07:57:39
问题 I'm trying to manually validate an object using its Data Annotations. Our models have the annotations in a nested class, so that they don't get overwritten when we automatically update them from the database schema. Let's say I have two classes: public class Person { [StringLength(20, MinimumLength = 3, ErrorMessage = "invalid length in Person.LOGIN")] public string Login { get; set; } } [MetadataType(typeof(Car_Metadata))] public class Car { public string Brand { get; set; } public class Car

C# ASP.NET Core ModelBinder not Updating Model

☆樱花仙子☆ 提交于 2019-12-24 07:58:34
问题 I have created a ModelBinder, which only gets triggered if an object has a [Decimal] attribute assigned, yet for some reason, despite it actually sanitising the data it does not seem to update the posted model. I wonder if someone could see from my code below, where I maybe going wrong. Startup.cs public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddMvc(config => config.ModelBinderProviders.Insert(0, new DecimalModelBinderProvider())); }

Exclude Fields From Model Validation

房东的猫 提交于 2019-12-23 13:04:39
问题 Let's say I have a following ViewModel : public class PersonViewModel { [Required] public String Email { get; set; } [Required] public String FirstName { get; set; } [Required] public String LastName { get; set; } } This is a ViewModel not a original Entity , I use this model in two places, in the first one I want to validate all fields, but in another one I want to exclude Email field from model validation. Is there anyway to specify to exclude field(s) from validation? 回答1: You can use

ASP.NET WEB API 2 - ModelBinding Firing twice per request

落花浮王杯 提交于 2019-12-22 18:45:24
问题 I have a custom validation attribute, that when I make a request to the server via a POST, is firing the IsValid method on the attribute twice. Its resulting in the error message returned to be duplicated. I've checked using Fiddler that the request is only ever fired once, so the situation is 1 request with model binding firing twice. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class MinimumAgeAttribute :

Validation error message not displaying MVC4

泪湿孤枕 提交于 2019-12-22 10:54:43
问题 I am using MVC4. Validation is failing but validation error messages are not getting displayed. This is my model. public class Configuration { public int Id { get; set; } [Required(AllowEmptyStrings = false, ErrorMessage = "Site name is required.")] [MinLength(6, ErrorMessage = "Name should be at least 6 characters.")] public string SiteName { get; set; } } Controller. [HttpPost] public ActionResult Create(Configuration configItem) { if (ModelState.IsValid) { // do something. } return View(

ASP.Net MVC 2 Model Validation Regex Validator fails

谁说胖子不能爱 提交于 2019-12-18 09:06:57
问题 I have following property in my Model Metadata class: [Required(ErrorMessage = "Spent On is required")] [RegularExpression(@"[0-1][0-9]/[0-3][0-9]/20[12][0-9]", ErrorMessage = "Please enter date in mm/dd/yyyy format")] [DataType(DataType.Date)] [DisplayName("Spent On")] public DateTime SpentOn { get; set; } But whenever I call ModelState.IsValid it always returns false because regex is not validating. I have matched the entered date (08/29/2010) against new regex using same pattern and it

ASP.Net MVC 2 Model Validation Regex Validator fails

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 09:06:26
问题 I have following property in my Model Metadata class: [Required(ErrorMessage = "Spent On is required")] [RegularExpression(@"[0-1][0-9]/[0-3][0-9]/20[12][0-9]", ErrorMessage = "Please enter date in mm/dd/yyyy format")] [DataType(DataType.Date)] [DisplayName("Spent On")] public DateTime SpentOn { get; set; } But whenever I call ModelState.IsValid it always returns false because regex is not validating. I have matched the entered date (08/29/2010) against new regex using same pattern and it

Model Validation / ASP.NET MVC 3 - Conditional Required Attribute

守給你的承諾、 提交于 2019-12-17 15:25:38
问题 I'm having trouble with my ASP.NET MVC 3 application. I have 2 propertiesin my model whereby I only want 1 of them required in my view based on whichever one is empty. So for example, if I enter a phone number then email is no longer required and vice versa, but if I leave both empty, then either 1 should be required, below is my model: [Display(Name = "Contact Phone Number:")] [MaxLength(150)] public string ContactPhoneNumber { get; set; } [Display(Name = "Contact Email Address:")]

ViewModel validation for a List

非 Y 不嫁゛ 提交于 2019-12-17 05:38:39
问题 I have the following viewmodel definition public class AccessRequestViewModel { public Request Request { get; private set; } public SelectList Buildings { get; private set; } public List<Person> Persons { get; private set; } } So in my application there must be at least 1 person for an access request. What approach might you use to validate? I don't want this validation to happen in my controller which would be simple to do. Is the only choice a custom validation attribute? Edit: Currently

C# string should not contain only white spaces or any special character except ,.';:" [duplicate]

不羁岁月 提交于 2019-12-13 08:56:50
问题 This question already has answers here : Combine two regular expression into one while validating Attribute (1 answer) How do I not allow special characters, but allow space in regex? (5 answers) Closed 10 months ago . I need a regular expression pattern to verify string does not contain only white spaces(blank with multiple space only)(Ex: " ".length = 4) and should not contain !@$#%^&*() characters. Regex regex = new Regex(@". \S+. "); This one checks for white spaces. I need both condition