-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Closed
Labels
Source - Docs.msDocs Customer feedback via GitHub IssueDocs Customer feedback via GitHub Issuedoc-bug
Description
I have encountered a tricky issue with using the polymorphic model binder example. Here are the steps I have performed:
- Created model hierarchy (BaseClass, DerivedClass1 -> BaseClass, DerivedClass2 -> BaseClass)
- Created custom model binder using the example on the page
- Added few validation attributes on the base and derived class
- Submitted completely valid model
- Received result:
{
"errors": {},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-9834a72009df584a8e0c3af809ea270c-afc6e5b8fa916f42-00"
}
It was very confusing because there is no error in the model, but the result is 400.
Finally, I've been able to resolve the issue using this answer - https://stackoverflow.com/a/64908680. It says that this page contains an error. instead of this
// Setting the ValidationState ensures properties on derived types are correctly
bindingContext.ValidationState[newBindingContext.Result] = new ValidationStateEntry
{ Metadata = modelMetadata };
should be this:
// Setting the ValidationState ensures properties on derived types are correctly
bindingContext.ValidationState[newBindingContext.Result.Model] = new ValidationStateEntry
{ Metadata = modelMetadata };
After making the changes above, validation started to work as expected.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: cfb00531-4ac6-8bf6-9bd8-1eed6446397f
- Version Independent ID: ed418d09-6c33-699a-0d48-9aabc99457be
- Content: Custom Model Binding in ASP.NET Core
- Content Source: aspnetcore/mvc/advanced/custom-model-binding.md
- Product: aspnet-core
- Technology: aspnetcore-mvc
- GitHub Login: @ardalis
- Microsoft Alias: riande
Metadata
Metadata
Assignees
Labels
Source - Docs.msDocs Customer feedback via GitHub IssueDocs Customer feedback via GitHub Issuedoc-bug