ModelState.IsValid == false, why?
About "can it be that 0 errors and IsValid == false": here's MVC source code from https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41
public bool IsValid {
get {
return Values.All(modelState => modelState.Errors.Count == 0);
}
}
Now, it looks like it can't be. Well, that's for ASP.NET MVC v1.
Why ModelState.IsValid always return false in mvc
Please post your Model Class.
To check the errors in your ModelState
use the following code:
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
OR: You can also use
var errors = ModelState.Values.SelectMany(v => v.Errors);
Place a break point at the above line and see what are the errors in your ModelState
.
ModelState.IsValid is false but should be true
The ModelState is already evaluated (and invalid) when entering your action, setting the "Number" property won't evaluate the model again. After setting the property, you may have to manually remove it from the model errors by writing this:
ModelState.Remove("Number");
ModelState.IsValid Return False Even There is No Error Why?
Look at your view and model again, you have put required on all your properties within the class
, however you are returning 7 of 9
properties. then Model.IsValid
will look at your Model
and see oh look there 2 required properties
that you are not returning
, so it will be false. either remove [required]
on those properties or add them within your view. good luck.
Update
Those 2 Properties are:
UserName
And Password
, they need to be within your Html.BeginForm
, so they will be send to server as the part of your class.
ModelState.IsValid == false
If your check:
if (ModelState.IsValid)
returns false, that most probably means that one of the required fields in your model, used in the view is populated with incorrect result. After you post your model the DateOfBirth
field seemed the most suspicious so removing the Required
attribute made us able to find out what cause the problem and to solve the problem temporary.
From now on you have two options - just leave it as it is (I do not recommend that) or make further investigation why you are not getting the expected data for that field. In other words, don't avoid the problems, but try to solve them and learn from them.
If my post helped you you may as well accept it as an answer.
ModelState.IsValid == false, although all model values are inserted
As for the first part of your question, ModelState
is populated by the DefaultModelBinder
when the method is first called. If property CuId
has the [Required]
attribute and its value is not posted back, then an error is added to ModelState
and therefore ModelState.IsValid
is false
. Just setting a property of your model does not remove ModelState
values.
As for the second part of your question, your not passing you model to the view in the GET method so @Html.HiddenFor(m => m.CuId)
generates a hidden input with no value (because the value of model.CuId
is null
or its default). All you currently doing is passing some values to the view using ViewBag
(not good practice) which you never even use. Instead, pass the model to the view as follows.
public ActionResult Create()
{
int lastcu = db.Cu.Max(l => l.Id);
var lastcuname = db.Customers.Find(lastcu).Name;
// Initialize a new instance of the model and set properties
Product model = new Product()
{
CuId = lastcu,
Cuname = lastcuname // assume this is a property of your model?
};
return View(model); // return the model
}
Side note: @Html.LabelFor(model => model.CuId, "Customer")
generates a html <label>
which is an accessibility element. Clicking on it sets focus to its associated form control. But you don't have an associated form control (just a hidden input which cannot receive focus)
ModelState.IsValid always returns false in ASP.NET Core 6.0 MVC
since you are using net 6.0 you have 2 choices
- Mark as nullable ALL properties in your ALL classes that not marked as required, for example
public string? Name { get; set; }
public string? Address { get; set; }
public string? Phone { get; set; }
... and so on
- OR change the project property Nullable , by commenting or removing or dissabling
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!--<Nullable>enable</Nullable>-->
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Related Topics
Refresh Datagridview When Updating Data Source
How to Tell JSON.Net Globally to Apply the Stringenumconverter to All Enums
How to Override a Non-Virtual Method
How Does MVC 4 List Model Binding Work
How to Access Winform Textbox Control from Another Class
How to Indefinitely Pause a Thread
Using Reflection in C# to Get Properties of a Nested Object
Using Image Control in Wpf to Display System.Drawing.Bitmap
Cross Platform (PHP to C# .Net) Encryption/Decryption with Rijndael
How to Get a JSON String from Url