dotnet / dotnet/aspnetcore

NullDisplayTextResourceType is not taken into account by DataAnnotationsMetadataProvider.

Open
#53,137 1 comment 1 reaction 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

ASP.NET Core 8.0 ignores NullDisplayTextResourceType in DataFormatAttribute:

```
[DisplayFormat(NullDisplayText = nameof(Resources.NullDate), NullDisplayTextResourceType = typeof(Resources))]
public DateTime? MyDate { get; set; }
...

MyDate: @Html.DisplayFor(m => m.MyDate)


```

The above code displays "MyDate: NullDate" instead of the actual string returned by Resources.NullDate.

The problem seems to be in [DataAnnotationsMetadataProvider.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.DataAnnotations/src/DataAnnotationsMetadataProvider.cs):

```
if (displayFormatAttribute != null)
{
displayMetadata.NullDisplayText = displayFormatAttribute.NullDisplayText;
// It should be displayMetadata.NullDisplayTextProvider = displayFormatAttribute.GetNullDisplayText;
}
```

See attached [WebApplication1.zip](https://github.com/dotnet/aspnetcore/files/13830407/WebApplication1.zip)

### Expected Behavior

DisplayFormatAttribute.NullDisplayTextResourceType should be taken into account.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.100

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.