Questions tagged [model-validation]

Model Validation is ASP.NET MVC validation method for your models with property attributes. Model validation works for client and server side validation.

For further information you can check Scott Gu's blog post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

There is a expansion called "DATA ANNOTATIONS EXTENSIONS" for other custom validation types such as CreditCard, min-max integer value etc. http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx

325 questions
3
votes
1 answer

ASP.NET MVC 2 Model Validation (DataAnnotations) on Subobject

Does the ASP.NET MVC 2 model validation includes subobjects? I have an instance "Filter", from this class: public class Filter { [StringLength(5)] String Text { get; set; } } in my main object: public class MainObject { public Filter…
Alex
  • 75,813
  • 86
  • 255
  • 348
3
votes
4 answers

Integer value model validation

I have a regular Integer (Not nullable) in my model: [Required] [Range(0, Int32.MaxValue - 1)] public int PersonId { get; set; } In my WebApi action, I accept an object that has that propery. public…
Ofiris
  • 6,047
  • 6
  • 35
  • 58
3
votes
1 answer

Rails and Model Validation

I want to put some model level validation on the following table: create_table :audios do |t| t.integer :library_id, :null => false t.string :file, :null => false, :limit => 2048 t.string :name, :limit => 512 t.timestamps end Does this…
Ash
  • 24,276
  • 34
  • 107
  • 152
3
votes
1 answer

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

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] …
3
votes
5 answers

Validation error message not displaying MVC4

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…
Naresh
  • 2,667
  • 13
  • 44
  • 69
3
votes
2 answers

Validation Attribute get triggered two times

In my MVC3 application I have the model ( not important properties deleted ): public class AccountViewModel { [StringLength(65)] public string Property1 { get; set; } [StringLength(65)] public string Property2 { get; set; } } The…
frm
  • 1,720
  • 1
  • 14
  • 18
2
votes
2 answers

The type of namespace 'Model' does not exist in the namespace

Today when I was going to create a partial view I got this error. The partial would contain a form and would be strongly typed with its corresponding model for validation. Although when debugging this through VS2010 I got an error. I checked my log…
2
votes
2 answers

Polymorphic binder: validation not working when custom model bound is wrapped in another class

I have changed a little the Polymorphic binding example from this article. Add [Required] attribute to the CPUIndex and ScreenSize properties of Laptop respectively SmartPhone classes. Run example and create whatever kind of device without filling…
Zoka
  • 2,312
  • 4
  • 23
  • 33
2
votes
3 answers

Model validation in CakePHP check that atleast one or the other is set

Is there a way to validate data (using CakePHP's model validation) to make sure that at least "a" or "b" has data (does not have to have data in both).
Dave
  • 28,833
  • 23
  • 113
  • 183
2
votes
2 answers

RequiredIf conditional attribute in .net Core

How to achieve RequiredIf conditional operation in .NET Core for Model based validation. In ASP.NET MVC, had used with ExpressiveAnnotation package but its not working for .NET Core.
2
votes
1 answer

RangeAttribute for DateTime validation not working in MVC web app

Using the RangeAttribute with a type of DateTime to to specify a minimum and maximum value for a date property does not work. A question on how to work around this has been asked here, with the highest-voted solution being: There is no need to…
devklick
  • 2,000
  • 3
  • 30
  • 47
2
votes
1 answer

ASP.NET Core Conditional Validation for controls

For example, I have these 3 properties in my view model public class PageViewModel { [Required] public bool? HasControl { get; set; } [Required] public bool? Critical { get; set; } …
Jackal
  • 3,359
  • 4
  • 33
  • 78
2
votes
2 answers

In asp.net mvc, how to add validations

I want to compare passwords and also a specified password length. There is some problem in the controller where I am updating it, it returns view only if the passwords are empty, then I applied condition to match them, but I want this to be done…
renu
  • 21
  • 7
2
votes
1 answer

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

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) …
2
votes
1 answer

Model Validation using StringLength validation attribute

In my ASP.NET MVC Core 1.1.1 I've a form with one input as follows where user is required to enter the district code as two characters such as 01, 02,...10,11, etc. But when I submit the form by entering district code as, say, 123, it still…
nam
  • 21,967
  • 37
  • 158
  • 332