model-validation

ModelState validation checking multiple boolean property

我与影子孤独终老i 提交于 2019-12-12 20:35:36
问题 I have view model that have multiple boolean properties, in my controller i have checking ModelState.IsValid before proceeding to service layer. Now I want to make that ModelState.IsValid return false if none of boolean property set to true, is there a way to make it happen? Here is my sample class public class Role { public int Id {get; set;} [Required(ErrorMessage = "Please enter a role name")] public string Name {get; set;} public bool IsCreator {get; set;} public bool IsEditor {get; set;}

ASP.NET Web API: model is valid if error message is set from resources

和自甴很熟 提交于 2019-12-12 15:24:23
问题 The problem is that in ApiController ModelState.IsValid is always true if I use .rsx file (Resources) to provide custom error message. Here is my model: public class LoginModel { public string Email { get; set; } [Required] [MinLength(5)] public string Password { get; set; } } Method in ApiController: [HttpPost] [ModelValidationFilter] public void Post(LoginModel model) { var a = ModelState.IsValid; } And the filter: public class ModelValidationFilterAttribute : ActionFilterAttribute { public

Custom validation rules for ASP.NET MVC2 Application

久未见 提交于 2019-12-12 05:00:48
问题 I am attempting to add validation to my application. I have some rules I need to check before allowing the information to be written to the database. I have the basic data validation added to the model, but I also need to make sure that if one field has a certain value, this other field is required. At one time the NerdDinner tutorial at asp.net covered that and I used that in the past for validation, but now I can't find that or any other example. Here is my model: public class DayRequested

ModelValidation on posted action parameter not happening

与世无争的帅哥 提交于 2019-12-12 04:02:57
问题 Why doesn't ASP.NET Core validate [FromBody] attributed action parameters? In the example below the paramter value of type SomeClass does not get validated. It doesn't even appear in the ModelState dictionary (only id ). this.ModelState.IsValid is always true , even though the Name property is set to a string longer than 2 letters. Even TryValidateModel is always true no matter what the request body contains (JSON). Sample Repo here public class Startup { public IConfigurationRoot

MVC validator for integer array in model

梦想与她 提交于 2019-12-11 22:17:43
问题 I have used validator required field in my model as follow and its working [Required(ErrorMessage = "Description is required.")] public string Description { get; set; } Now i have another property of integer array type public string[] Roles { get; set; } I am not able to get how can i put required field validator on integer array ? 回答1: Write a custom validation attribute. I've not tested but try code like this : public class RequiredArray : ValidationAttribute { public override bool IsValid

ASP.NET MVC 3 and validation attribute for dropdownlist with default value of 0

て烟熏妆下的殇ゞ 提交于 2019-12-11 19:54:37
问题 What is the best way to throw a required validation error using a validation attribute with a dropdownlist that has a default value of 0? If the value is 0, or the default value, I want the attribute to throw the error for my model. 回答1: you can provide the option label Docs that will set the selected value to 0 if not specified other wise like @Iridio menttioned in his answer, anotate the view model property with [Required] public class MyVieWModel { [Required] public int MyValue { get;set;}

cascading dropdown loses select items after post

江枫思渺然 提交于 2019-12-11 18:09:24
问题 I have an MVC3 razor project, which I am trying to build using multiple reusable elements. I have many model classes like the following public class ProductCreateModel { [Required] [Display(Name = "Org")] [UIHint("MyOrgsDropDown")] public string orgName { get; set; } [Required(ErrorMessage = "Select an account")] [Display(Name = "Account")] [UIHint("MyAccountsDropDown")] [AdditionalMetadata("orgFieldId", "orgName")] [AdditionalMetadata("fieldId", "accountName")] public string accountName {

Model normalization before model validation in Asp.Net Core 2.0+

人走茶凉 提交于 2019-12-11 17:40:06
问题 I'm using automatic model validation (see "Better Input Processing") to keep my controllers clean; so: [HttpPost] [ProducesResponseType(typeof(Product), 201)] public IActionResult Post([FromBody] Product product) { if (!ModelState.IsValid) { return BadRequest(ModelState); } product = _repository.AddProduct(product); return CreatedAtAction(nameof(Get), new { id = product.Id }, product); } becomes: [HttpPost] [ProducesResponseType(201)] public ActionResult<Product> Post(Product product) {

How to reset the formatted ErrorMessages of the validation attributes in ASP.NET MVC?

*爱你&永不变心* 提交于 2019-12-11 15:54:09
问题 I used a Custom validation attribute - AmountShouldBeLessOrEqualAttribute - that its validation process related to value of another property and this attribute works successfully . But in the following scenario I have a problem with it: Start the Application Going to the Form page Submit the Form (POST the form for first time ) The ModelBinding process cause that the value of ErrorMessage in the AmountShouldBeLessOrEqual attribute be formatted. For example: In the ViewModel there is an Amount

ASP.NET MVC 5 model validation for non-nullable types (Int32)

Deadly 提交于 2019-12-11 05:45:02
问题 I'm working on an ASP.NET MVC 5 application and the project owner is concerned about "under-posting" issues caused by validating non-nullable types (as mentioned in http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html and http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api). I created a test case to replicate this issue in ASP.NET MVC 5 but without luck. Model: public class ContactModel { [Required]