Skip to content

CompareAttribute refers wrong name value #94997

@rucio-rucio

Description

@rucio-rucio

Description

System.ComponentModel.DataAnnotations.CompareAttribute genrates wrong error message.
Because it don't referes DisplayName property of DisplayNameAttribute but Name property of DisplayAttribute.

Reproduction Steps

  1. Create new ASP.NET Core Web App Project On Visual Studio 2022 (17.8)
  2. Add new Razor Page named "test" into pages folder.
  3. code below in test.cshtml.cs
public class testModel : PageModel
{
    [BindProperty]
    [DisplayName("Parent's mail address")]
    [Display(Name = "xxxxx")]
    public string? email { get; set; }

    [BindProperty]
    [DisplayName("Confirm mail address")]
    [Compare(nameof(email), ErrorMessage = "Don't match with {1}")]
    public string? confirmEmail { get; set; }

    public void OnPost()
    {
        if (!ModelState.IsValid)
        {
            return;
        }
    }
}

4.code below in test.cshtml

<form method="post">
    <input asp-for="email" />
    <input asp-for="confirmEmail" />
    <span asp-validation-for="confirmEmail"></span>
    <input type="submit" />
</form>
  1. Run project and go to /test
  2. input "a" in first textbox, "b" in second textbox and click submit.

Expected behavior

It shows "Don't match with Parent's mail address" as error message.

Actual behavior

It shows "Don't match with xxxxx" as error message.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions