Blazor supports DisplayName for models#64636
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces the DisplayNameLabel<TValue> component to display property display names from metadata attributes ([Display] and [DisplayName]), providing Blazor with functionality similar to MVC's @Html.DisplayNameFor() helper. The component addresses a feature gap that previously required developers to hardcode labels or build custom reflection-based solutions.
Key Changes:
- New
DisplayNameLabel<TValue>component that extracts and displays property names from metadata attributes - Comprehensive unit tests covering attribute precedence, fallback behavior, and various property types
- E2E test validating browser rendering with real-world scenarios
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/Components/Web/src/Forms/DisplayNameLabel.cs |
Core component implementation with expression parsing and attribute reading logic |
src/Components/Web/test/Forms/DisplayNameLabelTest.cs |
Unit tests covering attribute precedence, type support, and error handling |
src/Components/test/E2ETest/Tests/FormsTest.cs |
Browser-based integration test verifying component rendering |
src/Components/test/testassets/BasicTestApp/FormsTest/DisplayNameLabelComponent.razor |
Test component demonstrating all attribute scenarios |
src/Components/test/testassets/BasicTestApp/Index.razor |
Navigation entry for the test component |
src/Components/Web/src/PublicAPI.Unshipped.txt |
Public API surface additions |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
The name |
...ates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Account/Pages/ResetPassword.razor
Show resolved
Hide resolved
javiercn
left a comment
There was a problem hiding this comment.
We need to ensure localization works with this component.
Added tests + tested multiple localizing resources manually. |
Sorry. I missed other feedback still open
I think the other feedback is not blocking:
@javiercn, I will do that now and move the discussion to there. |
Co-authored-by: Javier Calvarro Nelson <jacalvar@microsoft.com>
Summary
This PR adds the
DisplayName<TValue>component to enable displaying property display names from[Display]and[DisplayName]attributes in Blazor applications, addressing the feature gap identified in issue #49147.Background
Blazor currently lacks a built-in mechanism to display property names from metadata attributes like MVC's
@Html.DisplayNameFor()helper. This forces developers to either:The new
DisplayName<TValue>component provides an attribute-based solution that follows the same familiar pattern as other Blazor form components likeValidationMessage<TValue>.Fixes #49147
Changes
Public API
Implementation Details
The component extracts the property name from the expression and reads display metadata:
DisplayAttribute.Name(System.ComponentModel.DataAnnotations)DisplayNameAttribute.DisplayName(System.ComponentModel)Tests
Unit tests in
src/Components/Web/test/Forms/DisplayNameTest.cs:Forparameter is missingDisplayAttribute.NamecorrectlyDisplayNameAttribute.DisplayNamecorrectlyDisplayAttributetakes precedence overDisplayNameAttributeE2E test in
src/Components/test/E2ETest/Tests/FormsTest.cs:DisplayNamelReadsAttributesCorrectly()- Verifies the component renders correctly in a browser with all attribute scenariosAPI usage
Basic form usage:
renders e.g. as:
Table headers:
renders e.g. as:
Model example: