Skip to content
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

Add in-library support for DisplayFormat Attribute on Blazor model properties #41840

Closed
1 task done
spencerneutron opened this issue May 25, 2022 · 1 comment
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@spencerneutron
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I'd like to be able to use the MVC strategy using DisplayFormat attributes to format my model properties in Blazor apps.

Currently, the only native way to achieve this is to always apply .ToString(_format_) wherever the property is referenced in a Blazor page.

From 2019, it sounds like this is a known missing feature: Comment in Issue 5523

Currently:

Model.cs

public decimal MyDecimal { get; set; }

Page.razor

<div>
    @(${Model.MyDecimal:0.##})
</div>

OR with predefined formats like:

<div>
    @Model.MyDecimal.toString("N2")
</div>

Describe the solution you'd like

I'd like references to model properties decorated with the DisplayFormat attribute tag to reflect in Blazor pages.

Ideally, this just displays with the format string applied.

Model.cs

[DisplayFormat(DataFormatString = "{0:##}"
public decimal MyDecimal { get; set; }

Page.razor

<div>
    @Model.MyDecimal
</div>

Thanks!

Additional context

No response

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label May 25, 2022
@SteveSandersonMS
Copy link
Member

@deafcomedian Thanks for the suggestion.

We don't plan to make something like this happen by default whenever there's an @someExpression because it would be very breaking to existing code, plus even for new code there would be lots of cases where you don't want this to happen.

We'd recommend that you create some utility method that you can call, e.g., @MyFormatter(() => someExpression), which in turn can look up the formatting you want to apply. If the method is declared as accepting a Expression<Func<T>> then it's technically possible to determine the property attributes.

@SteveSandersonMS SteveSandersonMS added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label May 26, 2022
@ghost ghost added the Status: Resolved label May 26, 2022
@dotnet dotnet locked as resolved and limited conversation to collaborators Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants