-
Notifications
You must be signed in to change notification settings - Fork 10.3k
DisplayNameFor support in Blazor #49147
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
Comments
An attribute using a Resource key could be also useful to localize it. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Side-note on my hitting this, too, while performing doc updates. I was also going to cover it this way ... [Required, DisplayName("Production Date")]
public DateTime ProductionDate { get; set; } Both decorations ( I'm going to place some guidance on those approaches, but I'll comment it out so that it won't appear right now and leave a tracking note in place to keep an 👁️ on the issue. I can confirm that the <InputDate @bind-Value="Model!.ProductionDate" DisplayName="Production Date" /> |
I find it strange that decorating class members with attributes and using those attributes in code (so central and useful in Razor pages) has been abasically dropped for Blazor. I would suggest this should be pushed forward if possible. |
It doesnt look like it will make it it in .Net9 since its not in the planning doc, hoping for .Net 10...... |
Noting that there are related open issues (and a closed PR) on this subject ...
@danroth27 ... I hit this again for the new tutorial. I think subclassing |
Until this is supported out of the box, one can implement a custom component providing this functionality: @using System.Linq.Expressions
@using System.ComponentModel
@using System.Reflection
@using System.ComponentModel.DataAnnotations
@typeparam T
@displayName
@code {
[Parameter, EditorRequired]
public Expression<Func<T>> For { get; set; } = null!;
private string? displayName;
protected override void OnParametersSet()
{
displayName = GetDisplayName();
}
private string? GetDisplayName()
{
if (For is null || For.Body is null)
{
return null;
}
if (For.Body is not MemberExpression memExpr)
{
throw new InvalidOperationException($"The expression in parameter {nameof(For)} is not supported. Please use a lambda that accesses a property such as () => obj.PropertyA.");
}
if (memExpr.Member.GetCustomAttribute<DisplayAttribute>() is DisplayAttribute dAttr)
{
return dAttr.GetName();
}
if (memExpr.Member.GetCustomAttribute<DisplayNameAttribute>() is DisplayNameAttribute dnAttr)
{
return dnAttr.DisplayName;
}
return memExpr.Member.Name;
}
} See also this question on SO on the same topic. |
@danroth27 As mentioned in .NET 10 Server & APIs Planning Discussion pinging you to see if this can be considered for inclusion. |
@mguinness Thanks for the ping! This issue hasn't currently bubbled up to the top of our backlog, but if you haven't already, please be sure to give the original post a 👍 to indicate your interest. |
@captainsafia - Does any of the work done for #46349 cover |
MVC & Razor Pages have the DisplayNameFor HTML helper. Blazor doesn't currently have any helpers for creating a display name for model properties or expressions. This would be useful for scaffolding form field labels based on a model type, where you pick up the display name from attributes, like
[Display(Name="Release date")]
, on the model. QuickGrid has some logic for displaying column headings based on a model type, but it doesn't support theDisplayAttribute
.The text was updated successfully, but these errors were encountered: