Skip to content

Incorrect documentation for Html.HiddenFor: Html.HiddenFor will never use value property from htmlAttributes to assign value #49415

@alowdon

Description

@alowdon

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-null value found in: the ModelState entry with full name, the expression evaluated against Model, or the htmlAttributes dictionary 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-ui-renderingIncludes: MVC Views/Pages, Razor Views/PagesbugThis issue describes a behavior which is not expected - a bug.good first issueGood for newcomers.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions