-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Blazor supports DisplayName for models
#64636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to ensure localization works with this component.
Added tests + tested multiple localizing resources manually. |
| <label for="Input.NewPassword" class="form-label"> | ||
| <DisplayName For="() => Input.NewPassword" /> | ||
| </label> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should have Label rather than DisplayName or in addition to.
The main use case for this is to essentially create a label, which can happen in two ways
<label for="input-name">Label text</label>
<input name="input-name" />
Or
<label>
Label text
<input name="input-name" />
</label>
We don't need to tackle this here, but I would suggest filing a separate issue for it for us to consider. @danroth27 thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we did wanted to do this, we would also need to take into account Editor and HtmlFieldPrefix to compute the right name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, having a higher-level Label component in addition to DisplayName makes sense to me.
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. |
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:
Table headers:
Model example: