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.
Questions tagged [modelstate]
497 questions
5
votes
2 answers
How to deserialize 422 unprocessable entity error model and bind the errors to asp.net core razor pages model state
My Asp.Net Core 3.1 API returns 422 Unprocessable Entity error response as shown below:
{
"type": "https://test.com/modelvalidationproblem",
"title": "One or more model validation errors occurred.",
"status": 422,
"detail": "See the errors…

fingers10
- 6,675
- 10
- 49
- 87
5
votes
1 answer
Why does MVC use the Modelstate over a supplied Model on a GET
When MVC runs an ActionMethod it will populate the ModelState dictionary and uses a ModelBinder to build the ActionMethod parameter(s) if any. It does this for both GET and POST. Which makes sense.
After the ActionMethod successfully has been ran,…

Ric .Net
- 5,540
- 1
- 20
- 39
5
votes
9 answers
Asp.net MVC ModelState.Isvalid returning false for Id
I'm watching this ASP.NET MVC course. I have a customer model with these following attribute.
public class Customer {
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
…

Ahashan Alam Sojib
- 326
- 3
- 18
5
votes
1 answer
How can I manually bind data from a ModelStateDictionary to a presentation model with ASP.NET MVC?
I wrote an application using ASP.NET MVC 5 framework. I am using a two way binding between the views and the ViewModels.
Since I am using two way binding, I get the benefit of client and server side validation which is cool. However, when I send a…

Jaylen
- 39,043
- 40
- 128
- 221
5
votes
0 answers
Web API attribute routing and validation - possible?
I'm trying to combine attribute-based routing in Web API with model validation. I simply can't get it to work as I would expect.
class MyRequestModel
{
[DefaultValue("DefaultView")]
public string viewName { get; set; }
[Required]
…

Kieren Johnstone
- 41,277
- 16
- 94
- 144
5
votes
2 answers
If a key does not exist in the ModelState, how can I add it? aspnetmvc1
I am trying to create a workaround in my controller which handles a bug in ASP.NET MVC v1. The bug occurs if you post a listbox which has nothing selected (http://forums.asp.net/p/1384796/2940954.aspx).
Quick Explanation:
I have a report that…

BueKoW
- 926
- 5
- 18
- 33
5
votes
1 answer
ModelState create List string with Key + ErrorMessage. (LINQ)
I Want to obtain something like this:
' myKey: errorMessage '
Now i've a list with all the ModelState errors:
List modelStateErrors2 = ModelState.Keys.SelectMany(key => this.ModelState[key].Errors).Select(x => x.ErrorMessage).ToList();
But…

Ezequiel Leiva
- 75
- 1
- 8
5
votes
2 answers
Testing Model binding in ASP.NET MVC 2
First; I know that I should not need to test the internals of MVC but I REALLY need a suite a tests around data flowing into our system.
How can I, I hope without mocking all of HTTP context, test that objectA (form collection, dict, collection,…

detroitpro
- 3,853
- 4
- 35
- 64
5
votes
1 answer
How to add an error message to the ModelState on MVC using Javascript?
This is my Model property
public ModelStateDictionary modelSateClientSide { get; set; }
Now I called the Property in JavaScript and add the error in my ModelState
if (parseInt(academicAchievement, 10) > parseInt(peAcademicAchievement, 10))
{
…
user2578742
5
votes
1 answer
Hunting down ModelState.IsValid errors
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns false when I submit my form even if there aren't actually any fields in it, or anything being validated. I looked at this thread here and tried Steve Willcock's…

Graham Conzett
- 8,344
- 11
- 55
- 94
5
votes
1 answer
Partial Views & ModelState.AddModelError
the source of LoginRegister view is like this :
@Html.Partial("authentication/_login")
@Html.Partial("authentication/_register")
and each child view has got a form with this syntax
@using (Html.BeginForm(**seperated-methods**, "Login"))
{
…

Mironline
- 2,755
- 7
- 35
- 61
4
votes
1 answer
Clear field value if ModelState says the field is invalid
I want to clear the submitted value of a field in a model if the ModelState shows that the field is not valid.
This is where I have got so far but can't tie up the key to value in the model. Any suggestions?
if (!ModelState.IsValid)
{
foreach…

user482833
- 111
- 2
- 8
4
votes
4 answers
Use single error message template for all invalid properties
Imagine a razor page with a Form that have many inputs that user fills them.
with post method when it wants to validate the posted model like this :
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page(model);
…

nima ansari
- 434
- 9
- 19
4
votes
1 answer
How to use DataAnnotation attributes for validating a model in a console app
I'm attempting to use DataAnnotation attribute validation outside of an ASP.net MVC application. Ideally I'd like to take any model class in my console application and decorate it as follows:
private class MyExample
{
[Required]
public…

Rob
- 6,819
- 17
- 71
- 131
4
votes
3 answers
Override MVC Model Display Name Annotation with Custom Functionality
I have the following method where I read from a key-value XML file. I pass in a key and am returned a value where I used to display on my view.
public static class TextManager
{
public static string GetValue(string key)
{
string…

TroySteven
- 4,885
- 4
- 32
- 50