AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

The Enrollments field is required

Open savionlee opened this issue 4 years ago • 4 comments

After following this tutorial, I tried to edit a student, but kept being returned to the edit page, no error showing. image

After looking around online, i found this forum post that advised checking the ModelState. 🤔💭Weird, there were no model errors presented on my page to begin with.

So I looked for where Model errors were being rendered. After finding the helper tag on the scaffolded Edit.cshtml page: ... <div asp-validation-summary="ModelOnly" class="text-danger"></div> ... I changed it to this: ... <div asp-validation-summary="All" class="text-danger"></div> ...

Which finally showed me this error: The Enrollments field is required.

I checked my project settings and found that I left Nullable to enabled. I changed it to disabled, which allowed the TryUpdateModelAsync to pass true.

From what I understood when reading the document, because the TryUpdateModelAsync didn't specify an update to the Enrollments navigation property, it shouldn't have tried to update it in the first place.

With Nullable enabled, the TryUpdateModelAsync set the properties specified, then iterated through get on each, which threw the ModelError on the get of the Enrollments, which we didn't touch.

Is this an expected behavior and why?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

savionlee avatar Jan 08 '22 16:01 savionlee

This will be addressed in #27124

Rick-Anderson avatar Sep 30 '22 02:09 Rick-Anderson

I checked my project settings and found that I left Nullable to enabled. I changed it to disabled, which allowed the TryUpdateModelAsync to pass true.

Hi as per @savionlee's suggestion I can confirm this works on .NET 6 for me. I am more than happy to do a pull request with this page to include an alert for the user to disable nullable.

I will only do this with your permission first before I try to do a pull request, with credit to the OP of course. Thanks.

LayersOfAbstraction avatar Oct 10 '22 10:10 LayersOfAbstraction

@LayersOfAbstraction please do a PR

Rick-Anderson avatar Oct 10 '22 19:10 Rick-Anderson

We should change all asp-validation-summary from: ... <div asp-validation-summary="ModelOnly" class="text-danger"></div> ... to this: ... <div asp-validation-summary="All" class="text-danger"></div> ...

Rick-Anderson avatar Oct 14 '22 02:10 Rick-Anderson