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
535
votes
21 answers

How to get all Errors from ASP.Net MVC modelState?

I want to get all the error messages out of the modelState without knowing the key values. Looping through to grab all the error messages that the ModelState contains. How can I do this?
chobo2
  • 83,322
  • 195
  • 530
  • 832
216
votes
4 answers

ModelState.AddModelError - How can I add an error that isn't for a property?

I am checking my database in Create(FooViewModel fvm){...} to see if the fvm.prop1 and fvm.prop2 already exist in that combination; if so, I want to add an error to the modelstate, then return the whole view. I tried: public ActionResult…
Scott Baker
  • 10,013
  • 17
  • 56
  • 102
148
votes
10 answers

ModelState.IsValid == false, why?

Where can I find the list of errors of which make the ModelState invalid? I didn't see any errors property on the ModelState object.
Omu
  • 69,856
  • 92
  • 277
  • 407
137
votes
15 answers

ASP.NET MVC How to convert ModelState errors to json

How do you get a list of all ModelState error messages? I found this code to get all the keys: ( Returning a list of keys with ModelState errors) var errorKeys = (from item in ModelState where item.Value.Errors.Any() select…
JK.
  • 21,477
  • 35
  • 135
  • 214
126
votes
1 answer

How do I access the ModelState from within my View (aspx page)?

How do I access the ModelState from within my View (aspx page)?
Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56
121
votes
10 answers

Asp.net MVC ModelState.Clear

Can anyone give me a succinct definition of the role of ModelState in Asp.net MVC (or a link to one). In particular I need to know in what situations it is necessary or desirable to call ModelState.Clear(). Bit open ended huh... sorry, I think it…
Mr Grok
  • 3,876
  • 5
  • 31
  • 40
106
votes
12 answers

ASP.NET MVC - How to Preserve ModelState Errors Across RedirectToAction?

I have the following two action methods (simplified for question): [HttpGet] public ActionResult Create(string uniqueUri) { // get some stuff based on uniqueuri, set in ViewData. return View(); } [HttpPost] public ActionResult Create(Review…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
89
votes
7 answers

MVC3 Remove ModelState Errors

I've got a situation where I'm uploading an image the user has selected from his local file system. My Form in my view, basically has two submit buttons. One is used to Submit the form normally, and all validation executes. The 2nd is only for…
Jeff Reddy
  • 5,551
  • 9
  • 55
  • 88
52
votes
4 answers

What does ModelState.IsValid do?

When I do a create method i bind my object in the parameter and then I check if ModelState is valid so I add to the database: But when I need to change something before I add to the database (before I change it the ModelState couldn't be valid so I…
KhaoulaAtallah
  • 759
  • 1
  • 6
  • 16
50
votes
2 answers

ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

I have written my first unit tests for an ASP.NET MVC web application. All works fine and it is giving me valuable information, but I can't test errors in the view model. The ModelState.IsValid is always true, even when some values are not filled in…
CyclingFreak
  • 1,614
  • 2
  • 21
  • 40
47
votes
1 answer

How do I access the ModelState from an ActionFilter?

I'm building an ActionFilter to reuse some code for a simple spam block - basically what I do is that I have a Html Helper method that renders an input textbox and a hidden input, and in the ActionFilter I check whether the two values are the same…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
33
votes
1 answer

ModelState.IsValid is always returning false

[HttpPost] public ActionResult Create(Users user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } return View(user); } ModelState.IsValid is always…
Mizbella
  • 936
  • 2
  • 15
  • 30
32
votes
2 answers

Validation: How to inject A Model State wrapper with Ninject?

I was looking at this tutorial http://asp-umb.neudesic.com/mvc/tutorials/validating-with-a-service-layer--cs on how to wrap my validation data around a wrapper. I would like to use dependency inject though. I am using ninject 2.0 namespace…
chobo2
  • 83,322
  • 195
  • 530
  • 832
25
votes
7 answers

Testing ModelState is always valid in asp.net mvc

When testing my controller's actions the ModelState is always valid. public class Product { public int Id { get; set; } [Required] [StringLength(10)] public string Name { get; set; } [Required] public string Description {…
gdoron
  • 147,333
  • 58
  • 291
  • 367
24
votes
5 answers

Is it correct way to use ModelState.Remove to deal with ModelState?

Im working on a big MVC3 web application and have an annoyance regarding the ModelState.IsValid method. ModelState is being used in nearly all of my controllers so to validate the data being posted. The views are all based on ViewModels which…
Hesky
  • 787
  • 3
  • 8
  • 14
1
2 3
33 34