0

I am working on an ASP.NET Core MVC project. Currently, I'm trying to add actors from Create.cshtml view. The view has three fields and all of them are required:

  • ProfilePictureURL
  • FullName
  • Bio

I'm making these fields required with some data annotation attributes in the model.

In the controller, I am using [Bind] data annotation in the parentheses of the respective action method. Inside of its body I'm checking !ModelState.IsValid which is expected to be false. On the contrary, it's true and I don't get it why.

Here are some screenshots of model, controller and view.

Controller:

enter image description here

Model:

enter image description here

View:

enter image description here

Can anyone tell me why my !IsModelState.IsValid is always true?

Thanks in advance 😊

1
  • 1
    First of all, screenshot is not accepted to let other contributor expects to resolve your issue. Please share exact code snippet in order to investigate your concern. Commented Dec 7, 2022 at 1:38

1 Answer 1

1

Inside of its body I'm checking !ModelState.IsValid which is expected to be false. On the contrary, it's true and I don't get it why.

Altough, you haven't shared your code snippet in correct way and your asp.net core version information as well. However, if you use older version than asp.net core 6 in that case you might encounter this issue. Because, in older version of asp.net core when you use DataAnnotations in that scenario the property without any annotation doesn't take into account. In your case public List<Movie> Movies { get; set; } has no DataAnnotations which would comletely be ignored in older than asp.net core 6.

Debug in asp.net core 6:

enter image description here

Note: As you can see in asp.net core 6 as you haven't use any DataAnnotations on Movies property therefore, by default it will consider as [Required] and finally !ModelState.IsValid will be always false. You can check here

Debug in asp.net core Older: 3.XXX:

enter image description here

Note: Here !ModelState.IsValid always true because in older version it will consider as nun-nullable and always consider true. You can check here

Solution:

You should redefine your public List<Movie> Movies { get; set; } and tell what kind of behavior should be expected here. But in dot net core 6 or higher version, it will autometically consider as false not matter you use dataAnnotation

Sign up to request clarification or add additional context in comments.

4 Comments

big thanks. I'll keep in mind about the snippets 💯
Glad to assist you on this.
Yesterday, when I was writing this question, I was having mental break down due to this error. I was like John Malkovich from the Space Force movie - youtube.com/watch?v=2zpCOYkdvTQ 😁
I got your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.