question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModelState.IsValid == false with no errors

See original GitHub issue

In Version 7.3.3 I have a problem with a validator. In the Controller i can see ModelState.IsValid == false but no errors, because ModelState.ValidationState == Unvalidated .

Same validator works fine in version 7.3.2 (with exactly same data posted). I’m using fluent validator in an asp net core 2 application. The validator is the following PaymentModelEditEnrollValidator

   
   public class PaymentModelEditEnrollValidator : PaymentModelEditValidator<PaymentModelEditEnroll>
    {

    }

    public class PaymentModelEditValidator : PaymentModelEditValidator<PaymentModelEdit>
    {

    }

    public abstract class PaymentModelEditValidator<T> : AbstractValidator<T> where T : PaymentModelEdit
    {
        public PaymentModelEditValidator()
        {

            When(en => en.CanHaveExtraPayment, () =>
             {

                 When(en => en.Detail1.Amount.GetValueOrDefault(0) > 0, () =>
                 {
                     RuleFor(en => en.Detail1.ReasonId).NotNull().NotEmpty();
                     RuleFor(en => en.Detail1.TypeId).NotNull().NotEmpty();
                 });

                 When(en => en.Detail2.Amount.GetValueOrDefault(0) > 0, () =>
                 {
                     RuleFor(en => en.Detail2.ReasonId).NotNull().NotEmpty();
                     RuleFor(en => en.Detail2.TypeId).NotNull().NotEmpty();
                 });

                 When(en => en.Detail3 != null, () =>
                 {
                     When(en => en.Detail3.Amount.GetValueOrDefault(0) > 0, () =>
                     {
                         RuleFor(en => en.Detail3.ReasonId).NotNull().NotEmpty();
                         RuleFor(en => en.Detail3.TypeId).NotNull().NotEmpty();
                     });
                 });
                 When(en => en.Detail4 != null, () =>
                 {
                     When(en => en.Detail4.Amount.GetValueOrDefault(0) > 0, () =>
                      {
                          RuleFor(en => en.Detail4.ReasonId).NotNull().NotEmpty();
                          RuleFor(en => en.Detail4.TypeId).NotNull().NotEmpty();
                      });
                 });

               
             });
        }
    }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jan 2, 2018

@sid-6581 I’ve pushed out 7.3.4 which should fix the issue with shutting off native aspnet core validation (this only affected child collections, not root-level models).

0reactions
sid-6581commented, Jan 2, 2018

@JeremySkinner seems to have fixed it for me too!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ModelState.IsValid == false, why?
You can iterate through the keys on the dictionary and use the IsValidField method to check if that particular field is valid. Share....
Read more >
[Solved] ModelState.IsValid is always returning false in ...
I have a quick question I am updating my user and there role and my modelstate.isvalid is failing. So the user does not...
Read more >
Problem with ModelState.IsValid, return false where i ...
Hello, i have a problem with "ModelState.IsValid" ... IsValid to be true, but it false, because the ModelState. ... Is there any mistake?...
Read more >
Why ModelState.IsValid value getting false
Put a debug point on here 'if (ModelState.IsValid)' and after that check the all properties value inside the ModelState object.
Read more >
ModelState in ASP.NET MVC
IsValid is false if any of the properties submitted have any error messages attached to them. What all of this means is that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found