Skip to content

Html.HiddenFor will never use htmlAttributes value for value attribute #21314

@levdimov

Description

@levdimov

Describe the bug

Html.HiddenFor(m => m.Property) won't use htmlAttributes value for value attribute even if m.Property is null and/or there are no such key in ModelState.

To Reproduce

Model:

public class ViewForm
{
    [Required]
    public Guid? FormId { get; set; }
}

View:

...

@Html.HiddenFor(m => m.FormId, new Dicitonary<string, object> { { "value", Guid.NewGuid() } })

...

FormId is not initialized, so it is null.

Actual result:

<input data-val="true" data-val-required="The FormId field is required." id="FormId" name="FormId" type="hidden" value>

Expected result:

<input data-val="true" data-val-required="The FormId field is required." id="FormId" name="FormId" type="hidden" value="2c0d312b-b21b-4ee3-9b8a-5b8790632f6d">

Documentation for HiddenFor clearly states:

Returns an element of type "hidden" for the specified expression. Adds 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". See NameFor(Expression<Func<TModel,TResult>>) for more information about a "full name".

Further technical details

  • ASP.NET Core 2.1.* nuget packages @ net472

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions