-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Multiple form inputs with the same name render incorrect output. #52150
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
Comments
The same behaviour can be exhibited with a simple textbox: Model:
Controller:
View:
Change the value of the Input field before submitting. This value will override the value set by the controller. The same behaviour is exhibited if we use a model expression:
|
I have created a project to demonstrate this bug:
![]() Although the correct value is passed to the renderer, if the ModelState contains a value with the same name, the model state value is used. I suspect that we should try to get the value from the model state only if the value was not provided to the method? Maybe something like this would work: ![]() |
Making the change to favor the given value instead of the model state value breaks the test Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperHiddenTest.HiddenFor_UsesModelStateValueOverPropertyValue. So it seems intentionnal to always use the model state value if it is defined. |
@mikeedwards-rs thanks for contacting us. This is very much by design. ASP.NET Core uses the value from the form over the value from the property when rendering forms so that they roundtrip in the event of errors and the information is not lost. Otherwise if there are errors that produce an empty model, the form will be cleaned out and the user will have to re-add all the data. The problem with your model is that you are mixing input and output, and that only work if the input and output are the same. In your case, you have multiple output values that are aggregated into a single input value. You have two ways of resolving this:
|
It would be convenient to be able to render any field by giving it an explicit value without this value being overriden by what is in the model state. You can craft your models to try to avoid it, but this is a lurking side effect that can strike when you don't expect it as your models grow in complexity. For the use case of the empty model on error, would it have been possible to override the given value by the model state only if the given value was null? Otherwise, when I do not need the model state to render a field, it would be convenient to add a parameter when calling the renderer to force using the given value. It is a bit confusing that there is already a parameter called "isExplicitValue", but this doesn't mean to prefer value instead of the model state it seems. The best workarounds I can think of right now (in term of convenience) to avoid the issue are: |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Is there an existing issue for this?
Describe the bug
When rendering an array of values as input fields. .NET core does not correctly render the values of those fields after a post back.
Instead .NET Core picks the first value that matches the field name from the POST request and applies it to all inputs with that name. It ignores the value explicitly passed in by the code.
Expected Behavior
The expected behaviour is that all the fields after the post back will have the correct A to D values.
Somewhere some state is overriding the values set in the code.
Steps To Reproduce
Create a controller with the following method:
Which is then render with the following view:
On the first request the page renders as expected, each textbox as the correct letter:
However after clicking the submit button you will get the following result:
Now all the inputs contain the Value A instead of A to D, despite an explicit value being passed to the helper method.
Exceptions (if any)
No response
.NET Version
7.0.404
Anything else?
ASP.NET 7.0.14
The text was updated successfully, but these errors were encountered: