dotnet / dotnet/aspnetcore

Allow access to `GetDisplayName` for components

Open
#65,108 2 comments 0 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

[DisplayNameFor support in Blazor](https://github.com/dotnet/aspnetcore/issues/49147) issue was closed in https://github.com/dotnet/aspnetcore/pull/64636. It provides the new `DisplayName` component and the internal [ExpressionMemberAccessor](https://github.com/dotnet/aspnetcore/blob/main/src/Components/Web/src/Forms/ExpressionMemberAccessor.cs) class enables it to retrieve the display name for a model property:

1. First checks for `DisplayAttribute.Name` (System.ComponentModel.DataAnnotations)
2. Falls back to `DisplayNameAttribute.DisplayName` (System.ComponentModel)
3. Uses the property name itself if no attributes are present

However, other components don't have support and will still have the following issues:

* Hardcode label text (violating DRY principles and making localization difficult)
* Build custom reflection-based solutions
* Duplicate display name information across models and views

This is apparent in both the QuickGrid component needing custom code for [Display name support](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/quickgrid#display-name-support) and also [InputBase](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.inputbase-1) using hardcoded [Display name support](https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/input-components#display-name-support).

In addition, third-party components also have the same problem in libraries like DevExpress, SyncFusion, Telerik etc. that all need to show the display name for UI. The issue [MudDataGrid: Support DisplayAttribute for PropertyColumn title](https://github.com/MudBlazor/MudBlazor/issues/6538) demonstrates the need for commonality.

The proposal would mean opening up access to `GetDisplayName` in the `ExpressionMemberAccessor` class. It's such a common use case that should be unified within Blazor, instead of separate implementations. Also consider moving namespace to `Microsoft.AspNetCore.Components` as this functionality isn't restricted to forms.

Contributor guide

Open the contributing guide

Research direction

Start with src/Components/Web/src/Forms/ExpressionMemberAccessor.cs and review how the DisplayName component accesses GetDisplayName. Compare the QuickGrid and InputBase display-name support described in the issue, then determine the appropriate public API and namespace. Done means other components can use the shared display-name behavior without custom reflection-based implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.