Skip to content

Model client validation css styles not work well with @Html.EditorFor #30799

Open
@Ethan-oss-dot

Description

@Ethan-oss-dot

Describe the bug

If use @Html.EditorFor with the expression for a property, client-side validation will display error message with wrong css style.

image

The class attribute has no value

image

To Reproduce

@for (var i = 0; i < Model.TestChildModels.Count; i++)
    {
        @Html.EditorFor(model => model.TestChildModels[i])
    }

EditorTemplate/TestChildModel.cshtml

@model TestChildModel

<div class="row">
    <div class="col-4">
        <input asp-for="Id" />
        <span asp-validation-for="Id" class="text-danger"></span>
    </div>
    <div class="col-4">
        <input asp-for="Name" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="col-4">
        <input asp-for="Count" />
        <span asp-validation-for="Count" class="text-danger"></span>
    </div>
</div>

The workaround I am using now

add following code snippet in site.css

span.field-validation-error span {
    color: red;
}

Further technical details

  • ASP.NET Core version 5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-razor-viewsFeatures related to the Razor view engine for Razor pages and MVC viewsinvestigate

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions