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
4
votes
2 answers
Access the error message in ModelState error dictionary in ASP.net MVC unit test
I have added a key-value pair in the action result like this:
[HttpPost, Authorize]
public ActionResult ListFacilities(int countryid)
{
...
ModelState.AddModelError("Error","No facilities reported in this country!");
...
}
I have…

Seen
- 4,054
- 4
- 37
- 46
4
votes
1 answer
How to return ModelState with Forbidden status web api
We can return ModelState with BadRequest from web api in following way:
return BadRequest(ModelState);
It provides following output:
{
"Message": "The request is invalid.",
"ModelState": {
"property": [
"error"
…

ctor
- 805
- 1
- 10
- 26
4
votes
1 answer
Model is not automatically validated when unit testing
Here's part of a controller action:
[HttpPost]
public ActionResult NewComplaint(Complaint complaint)
{
if(!ModelState.IsValid)
{
// some code
}
// some more code...
}
When running the application, the model is automatically…

xTRUMANx
- 1,035
- 1
- 8
- 12
4
votes
2 answers
Asp.Net core WebApi modelstate not working
I have this webapi and trying to use the "modelstate.IsValid" function, but i have ran into some strange problem.
I have the following method in my controller:
[HttpPut]
[Route("{id}")]
public async Task…

mike
- 143
- 2
- 8
4
votes
1 answer
ASP.NET MVC auto-binds a refreshed model when ModelState is invalid on HttpPost
I'm working on an ASP.NET MVC2 app. I've come to realize a very surprising, yet amazing thing that MVC does behind the scenes having to do with the ModelState and model binding. I have a ViewModel which has a whole bunch of data - some fields being…

tbehunin
- 1,043
- 1
- 12
- 24
4
votes
0 answers
Web API Model validation with DataMember Name attribute
I am using [DataMember(Name="property_name")] attributes for properties on my models which are decorated with [DataContract]. In addition to that I use the [Required] attribute on each required property for validation. I have a registered an…

Lukas
- 694
- 1
- 9
- 24
4
votes
1 answer
Updating value provider prior to TryUpdateModel
Lets say we have a class with a property called PetsName. If it is left blank on the screen I want to update the value provider so if the user doesn't enter a pet name, we force 'unnamed'. This isn't the actual scenario.. this is of course a sample,…

Adam Tuliper
- 29,982
- 4
- 53
- 71
4
votes
1 answer
ASP.NET MVC: how to add nested property to Modelstate?
I'd like to add a property to the Modelstate when it's not valid, but I'm not sure how to do that when the property is nested inside another.
Below is what Visual Studio already did for me.
…

Richard77
- 20,343
- 46
- 150
- 252
4
votes
2 answers
How to get the error message from model state
I'm using
modelstate.Adderror("test","test message")
And how can i get this modelstate value in controller itself.
Like I need to get the error message of "test" in the controller.

Santhosh
- 19,616
- 22
- 63
- 74
4
votes
1 answer
MVC3 Add & Display Message through ModelState
I have a scenario like i need to display error message which is coming from DB on Edit [GET] request.
I know this can be done if request type is of [POST], but how can we do this in [GET] request.
Same Code:
[HttpGet]
public ActionResult…

Sham
- 691
- 2
- 13
- 26
4
votes
1 answer
How can I replace an entity by another entity with the same key in an Entity Framework DB first approach?
I am programming an MVC 4 webapplication using EF 5 Database first. I have some apparently trivial issues for which I cannot find a proper solution. These issues are with the object state manager.
In the simplest scenario, everything works fine: I…

R. Schreurs
- 8,587
- 5
- 43
- 62
4
votes
2 answers
ModelState.AddModelError - no error shown
I'd like to set an error message for an email field after an exception has been caught in my controller:
...
catch (EmailAlreadyExistsException emailAlreadyExistsException)
{
ModelState.AddModelError("Useraccount_Email", "This is an error…

mosquito87
- 4,270
- 11
- 46
- 77
4
votes
2 answers
ASP.NET MVC Model Validation Error Localization Context
First of all, I have to say that I understand how Data Annotation -based Model Validation works in ASP.NET MVC4 and I have it successfully implemented with DataAnnotationsModelValidatorProvider. So I don't need assistance on setting it up.
But when…

Jani Hyytiäinen
- 5,293
- 36
- 45
4
votes
2 answers
Data annotations MVC3 Required attribute
I have the Model (User) below, I use it to add new users and to update existing users.
When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's…

Sah
- 189
- 1
- 2
- 11
4
votes
1 answer
ASP .NET MVC 4 View containing two partial Views with a form in each
In my MVC 4 application I have an Index View. In this View I have two div tags where a Partial view is rendered. There are two buttons that when clicked toggles between these two divs using jQuery. The Partial Views are strongly typed with its model…

Javid
- 821
- 2
- 8
- 13