Skip to content

Data annotations localization does not appear to work with positional records #39551

Open
@smfeest

Description

@smfeest

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using model binding and data annotations localization with positional records, the error messages are not localized (whereas as they localised when exactly the same attributes are used with non-positional records).

For example, consider the two broadly equivalent record types that could be used as the model for a form:

// version 1: Non-positional record
public record GreetingViewModel
{
    [Required(ErrorMessage = "Error_NameRequired")]
    public string? Name { get; init; }
}

// version 2: Positional record
public record GreetingViewModel([Required(ErrorMessage = "Error_NameRequired")] string? Name);

Together with the following resource file:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <data name="Error_NameRequired">
    <value>Please enter your name</value>
  </data>
</root>

When the form is submitted without specifying a name and we use the non-positional version of the model, the validation error message is correctly displayed as "Please enter your name". However when we use the positional version of the model the error message is incorrectly displayed as "Error_NameRequired".

Expected Behavior

Error messages are localized regardless of whether the record is positional or not.

Steps To Reproduce

  1. Clone the minimalist repro project at https://github.com/smfeest/repro-positional-record-attribute-localization
  2. Run the application and navigate to http://localhost:5251 or https://localhost:7045/
  3. Submit the form without entering a name
  4. The unlocalized validation error message ("Error_NameRequired") is unexpectedly displayed
  5. Comment out the positional version of GreetingViewModel in Models/GreetingViewModel.cs and uncomment the non-positional version below it
  6. Restart the application
  7. Once gain, submit the form without entering a name
  8. The localized validation error ("Please enter your name") is now correctly displayed

Exceptions (if any)

No response

.NET Version

6.0.101

Anything else?

Output of dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.101/

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  5.0.404 [/usr/share/dotnet/sdk]
  6.0.101 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.13 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.13 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions