Questions tagged [modelstate]

The ModelState ( System.Web.MVC.ModelState ) is a component in the ASP.Net MVC Framework that manages the state of the Model through Views and Controller classes.

497 questions
9
votes
2 answers

ModelState is always considered valid, regardless of null values in required fields

I've been looking around and I think my solution is just fine but somehow the ModelState.IsValid property is always true. Consider the following code snippets: [Route("address")] [HttpPut] [ResponseType(typeof(UserViewModel))] public…
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
8
votes
1 answer

What is the difference between ModelError and ValidationResult?

In ASP.NET MVC there exists a ModelState class that contains ModelErrorCollection. And a ModelError represents an error the occurs during model binding. I know that ValidationResult is returned from ValidationAttribute.IsValid Method, and validates…
Michael R
  • 1,547
  • 1
  • 19
  • 27
7
votes
1 answer

How to set ModelState true from controller action

How to set ModelState = true; in asp.net MVC from controller action as we know ModelState.IsValid is readOnly ie. holds getter only, So we can't force our modelState to true like thisModelState.Isvalid = true; //what we can't do Now do tell me guys…
RollerCosta
  • 5,020
  • 9
  • 53
  • 71
7
votes
1 answer

ASP.Net Core: How do you get the key of an invalid ModelState value?

II have an "Edit" page in my .Net 5./ASP.Net MVC app. If ModelState.IsValid is "false", I want to check the individual errors before rejecting the entire page. PROBLEM: How do I get the "name" of an invalid item in the ModelState list? For…
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
7
votes
2 answers

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property…
7
votes
2 answers

How to set Modelstate error keys to camel case?

How do I set the modelstate keys to camel case in WEB Api .net framework. I use JsonProperty attribute to set the property names to camel case. Now I want the modelstate to be the same as the json (camel case) how do I achieve that?
7
votes
5 answers

ASP.MVC 2 RTM + ModelState Error at Id property

I have this classes: public class GroupMetadata { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] public string Name { get; set; } } [MetadataType(typeof(GrupoMetadata))] public partial class Group { …
Zote
  • 5,343
  • 5
  • 41
  • 43
7
votes
1 answer

jQuery Validator.showErrors MVC ViewModel ModelState

Basically I'm having trouble showing modelstate errors returned from the controller (WebApi). Using MVC4,jQuery and knockout. Hopefully you can see what I am trying to achieve from the below - thanks in advance. View:-
renz
  • 165
  • 3
  • 12
7
votes
3 answers

MVC 4. ModelState.IsValid always return true

I don't understand why ModelState.isValid give me in all the ways. I set something in the email returns true and I pùt empty field, it returns true too. My question ism, what do I have to do to return true when the field is empty and nothing whn I…
Dave
  • 7,028
  • 11
  • 35
  • 58
7
votes
1 answer

Returning HttpPostedFileBase to view on validation error

When I attempt to upload images to my MVC controller action and there is a validation error, I have to click through each of the buttons and find all of my files again. If I have a view that consists of
kevskree
  • 4,442
  • 3
  • 24
  • 32
6
votes
1 answer

MVC 3 ModelState.IsValid with multiple ViewModels

Let's just say I have A ViewModel that is made up of 3 other ViewModels. One contains a list of items, the other contains an instance of a class with a [Required] attribute and then another list of other items. If the user selects from one of the…
ewahner
  • 1,149
  • 2
  • 11
  • 23
6
votes
1 answer

How to determine which ValidationAttribute returned a ModelError

I'm having an issue where I am trying to determine which ValidationAttribute returned a particular ModelError. I have an endpoint in my web api that takes a model such as; public class MyClass { [Required] [Range(0, 3)] public int?…
Scanlam
  • 61
  • 4
6
votes
4 answers

How to replace the default ModelState error message in Asp.net MVC 2?

I need to replace the model state resource (to another language). I've seen some answers to the question above, but unfortunately I could'nt make it work. Any detailed answer or example would be appriciated. Thank you.
Godfa
5
votes
2 answers

Returning a list of keys with ModelState errors

How can I return a list/array of all keys that have an error? I have tried to do the below, but it says I can't have that sort of expression for some reason. ModelState.ToList(item => item.Value.Errors.Count > 0)
Jon
  • 38,814
  • 81
  • 233
  • 382
5
votes
1 answer

Check ModelState in action filter

Can I check ModelState.IsValid in my custom action filter in OnActionExecuting method?
Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
1 2
3
33 34