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
22
votes
3 answers

How to do model validation in every method in ASP.NET Core Web API?

I am getting into ASP.NET Core 2.0 with Web API. One of my first methods are my login: /// /// API endpoint to login a user /// /// The login data /// Unauthorizied if the login fails, The jwt…
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
22
votes
2 answers

How to get a ModelState key of an item in a list

Problem I have a list of fields that the user can edit. When the model is submitted I want to check if this items are valid. I can't use data notations because each field has a different validation process that I will not know until runtime. If the…
Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82
21
votes
6 answers

How to figure out which key of ModelState has error

How do I figure out which of the keys in ModelState that contains an error when ModelState.IsValid is false? Usually I would just hover the mouse thru the ModelState.Values list checking item by item for error count > 0. But now I'm working on a…
leobelones
  • 578
  • 1
  • 8
  • 24
19
votes
2 answers

How do I add modelstate error to a list

I have a view that uses a list of modelitems like this: List When I get this list serverside I check if this is one type of item, it has to have a valid serial number. If its another…
devzero
  • 2,510
  • 4
  • 35
  • 55
19
votes
3 answers

Success messages as opposed to model state error messages

For error messages, validation faults etc you have ModelState.AddErrorMessage("Fool!"); But, where do you put success responses like "You successfully transfered alot of money to your ex." + "Your balance is now zero". I still want to set it at…
Martin
  • 2,956
  • 7
  • 30
  • 59
16
votes
5 answers

ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

I have tried to make this work and made many google/stackoverflow searches with no luck at all. I have a simple Model: public class MovieModel { public string Id { get; set; } [Required] [StringLength(100)] public string Name { get;…
André Baptista
  • 490
  • 8
  • 23
16
votes
6 answers

How to read modelstate errors when returned by Json?

How can I display ModelState errors returned by JSON? I want to do something like this: if (!ValidateLogOn(Name, currentPassword)) { ModelState.AddModelError("_FORM", "Username or password is incorrect."); //Return a json…
learning
  • 11,415
  • 35
  • 87
  • 154
15
votes
2 answers

Refresh ModelState to remove errors

Refreshing the ModelState Hi, I have a question about the ModelState in an ASP.NET MVC controller. When the user selects a certain option from the view, the start date and end date for the "certification" will be set based on the other dates…
HermanTheSheep
  • 183
  • 1
  • 2
  • 11
14
votes
3 answers

Is there a strongly-named way to remove ModelState errors in ASP.NET MVC

Is there a way to remove ModelState errors during an ASP.NET MVC postback without having to write each one by hand. Let's say we have a checkbox Billing Same As Shipping and we want to then ignore anything user wrote for ShippingAddress when it's…
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
13
votes
4 answers

asp.net mvc without entity framework

I am learning asp.net mvc and went through a great tutorial that demonstrated it. The tutorial also used Entity Framework. We have our own data access class which I have to use. I am a little bit confused as to what I need to do to bridge the gap…
sarsnake
  • 26,667
  • 58
  • 180
  • 286
13
votes
2 answers

How to render errors to client? AngularJS/WebApi ModelState

I'm building an AngularJS SPA application with WebApi for the backend. I am using attributes for model validation on the server, if validation fails this is what I return from the ModelState. {"Message":"The request is…
Brad Martin
  • 5,637
  • 4
  • 28
  • 44
13
votes
1 answer

Multiple forms in MVC view: ModelState applied to all forms

Running into some trouble with multiple forms on a single view. Suppose I have the following viewmodel: public class ChangeBankAccountViewModel { public IEnumerable BankInfos { get; set; } } public class BankInfo { …
Kippie
  • 3,760
  • 3
  • 23
  • 38
13
votes
2 answers

What is the ModelState class in MVC 3?

I am learning MVC, and in ASP.Net MVC 3, what is the ModelState class ? I have looked on Google and MSDN, but I can't seem to get a clear understanding of it's purpose. Can anyone help?
Frank
  • 2,015
  • 10
  • 36
  • 57
12
votes
3 answers

C# .NET MVC3 ModelState.IsValid

Im using JSON to post data from a form and ModelState.isValid() returning false, i put a WriteLine for all incoming data and everything looks fine data wise, is there a way to display model state errors to figure out what is not validating? this…
z.eljayyo
  • 1,289
  • 1
  • 10
  • 16
9
votes
1 answer

Custom model binding, model state, and data annotations

I have a few questions regarding custom model binding, model state, and data annotations. 1) Is it redundant to do validation in the custom model binder if I have data annotations on my model, because that's what I thought the point of data…
1
2
3
33 34