-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OrchardCore.Forms throws InvalidOperationException when restoring checkboxes from ModelState #16975
Comments
Just remembered something like this happened before #7256 |
Do you think you can provide a PR fixing the conversion? |
We triaged this issue and set the milestone according to the priority we think is appropriate (see the docs on how we triage and prioritize issues). This indicates when the core team may start working on it. However, if you'd like to contribute, we'd warmly welcome you to do that anytime. See our guide on contributions here. |
I started looking into a fix, however I was unable to reproduce my problem exactly in stock OC. I didn't get the exception, but also the checkbox state was not preserved. If I checked the box and submitted the form, I got redirected back but the checkbox remained unchecked. Might be a different symptom but the same underlying cause. Need to look into it more. |
Stock OC example from the main branch
If you get stuck I modified the example "contact" form from the OC docs Submit the form. The text input state is preserved. The checkbox state is not preserved. Maybe this is a different symptom of the same problem. I don't get the InvalidOperationException, although my checkbox state is also not preserved for the invalid form. |
Apologies everyone. The original issue I submitted may be in my own code. But I do think there is an OC bug with checkbox state not being preserved in OrchardCore.Forms. I'm going to submit a new issue and PR for that. |
Thanks for investigating! |
Describe the bug
OrchardCore.Forms
leveragesModelStateHelpers.SerializeModelState
andModelStateHelpers.DeserializeModelState
to preserve ModelState across web requests. This is broken for checkboxes sinceSystem.Text.Json
was introduced in 2.0.0.Orchard Core version
2.0.2
To Reproduce
In my app I'm not actually using the form content item, but I do use
OrchardCore.Forms
solely for the ModelState preservation "trick". But I imagine you can reproduce this in OC with a simple form content item with a checkbox. You also need the form to be invalid so the ModelState preservation trick gets invoked and the user gets redirected back to the form.Here's a simple console app that demonstrates the issue by comparing what happens with Newtonsoft.Json to System.Text.Json.
Running this console app produces the following output:
The key is this line in ModelStateHelpers.
The Newtonsoft.Json version of the key line is
The STJ version of the key line is
is JArray
is true for a checkbox, so the value is converted toobject[]
.is JsonArray
is false for a checkbox, so the value is left asJsonArray
.Over in .NET we look at ModelBindingHelper.UnwrapPossibleArrayType. With STJ we end up passing down
List<object>
, which it does not know how to unwrap and so it falls into case 4. Then we get an exception because it doesn't know to convert a list to a checkbox.The text was updated successfully, but these errors were encountered: