Razor Pages ComplexTypeModelBinder fails to handle the suggestion of "Alternatively, set the 'X' property to a non-null value in the 'YModel' constructor" #21916
Labels
affected-very-few
This issue impacts very few customers
bug
This issue describes a behavior which is not expected - a bug.
feature-model-binding
investigate
old-area-web-frameworks-do-not-use
*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
severity-nice-to-have
This label is used by an internal tool
Milestone
Version
.NET Core 3.1
Microsoft.AspNetCore Nuget Packages 3.1.4 (latest)
Context of the problem
I have a page model with five properties that are a complex type, which is need to be bound on post back, this is for making an async upload mechanism, that will show a button for selecting the file and a button for uploading, when this one is clicked we show a progress bar, lock the buttons, finish the upload and roll back those UI changes, and also change a feedback icon so that the user knows that the file had been uploaded. When the file is completely loaded to the server, a hidden checkbox is marked, that's the only thing need to be validated by the JQuery Unobtrusive validation (a custom one). The Filename is just so that the user can read what was the file that he uploaded.
The objective of all this is the abstraction of how I upload a file, and not customizing code for every file upload i need to build, knowing a mapping to the internal file i need to upload, and some feedback to the user. The DocCode is part of ajax posting the file to another page handler.
I also need the Name property because of that passing a property as the new model for the partial view, if that property has a DisplayAttribute it can't be used so i have to send it in someway to the View for rendering.
Sample Code
** some things have been cut out for simplicity **
the Document type is defined this way:
I have a View that receives this model
and the page, includes this partial view five times in this way:
Tracking the problem
When posting back II receive a binding error on a Razor Page when posting a form (post method): "Could not create an instance of type 'Document'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the 'IDDoc' property to a non-null value in the 'DocumentsModel' constructor."
The property has an instance set to it, but the ParameterBinder gets to this line:
aspnetcore/src/Mvc/Mvc.Core/src/ModelBinding/ParameterBinder.cs
Line 132 in c565386
And creates a DefaultModelBindingContext which the Model is set to NULL, then when calling, on ComplexTypeModelBinder.BindModelCoreAsync we get to this line:
aspnetcore/src/Mvc/Mvc.Core/src/ModelBinding/Binders/ComplexTypeModelBinder.cs
Line 109 in c565386
which is annotated with a comment as:
on the next line, it is called the CreateModel, since the provided Model is null, but the property really has a value, this method tries to create a new instance, where:
Correct functionality
If the property doesn't have a setter, then just validate that it has an instance, or else then try to constructor the object and fail if there is no parameterless constructor.
This is really dissapointing, I'd have to do a workaround with a ModelBinderAttribute or something like that.
The text was updated successfully, but these errors were encountered: