-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Re-opening #21314 as it was not resolved but is now closed for comment.
The documentation for Html.HelperFor explicitly states that the helper will
add a "value" attribute to the element containing the first non-
nullvalue found in: the ModelState entry with full name, theexpressionevaluated against Model, or thehtmlAttributesdictionary entry with key "value".
However this is not the behaviour seen when using Html.HelperFor in .NET 6 (the latest LTS at time of writing). Either the behaviour should be fixed, or the documentation should be updated to avoid misleading people.
Expected Behavior
With a model
public class Model
{
public string Foo { get; set; }
}calling the following in a Razor template
Html.HelperFor(m => m.Foo, new { value = "bar" })
should result in
<input id="Foo" name="Foo" type="hidden" value="bar">
Steps To Reproduce
- Create a new ASP.NET Core MVC project
- Create a model as shown above
- Create a view using the model, including
Html.HelperFor(m => m.Foo, new { value = "bar" }) - Return the view from a controller
The generated HTML will be
<input id="Foo" name="Foo" type="hidden" value="">
Note that value="", but the ModelState and model itself should both provide null values, so the value should instead be taken from the htmlAttributes (as detailed in the documentation), but is not.
Exceptions (if any)
No response
.NET Version
6.0
Anything else?
No response