aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

FormatStrings not working with @bind-value:format for <InputNumber> in Blazor WebAssembly

Open fingers10 opened this issue 4 years ago • 5 comments

Describe the bug

Display Formatting doesn't work with decimal types in Blazor WebAssembly.

To Reproduce

  1. Create a New Blazor Web Assembly App.
  2. Navigate to Index.razor
  3. Paste the below code.
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputNumber id="name" @bind-Value="exampleModel.Price" @bind-Value:format="{0:#.##}" />

    <button type="submit">Submit</button>
</EditForm>

@code
{
    protected ExampleModel exampleModel = new ExampleModel
    {
        Price = 580.471m
    };

    protected void HandleValidSubmit()
    {
    }

    public class ExampleModel
    {
        //[DisplayFormat(DataFormatString = "{0:#.##}", ApplyFormatInEditMode = true)]
        public decimal Price { get; set; }
    }
}
  1. Notice the output as shown below: image

  2. @bind-value:format doesn't seem to work. I have also tried with [DisplayFormat(DataFormatString = "{0:#.##}", ApplyFormatInEditMode = true)] Data Annotation. This also doesn't work.

  3. Later from the docs I noticed that

Data binding works with DateTime format strings using @bind:format. Other format expressions, such as currency or number formats, aren't available at this time.

Is there any plans on including this in preview release? Please can you share an update on this? Or is there any best workaround for this at the moment?

Further technical details

  • ASP.NET Core version - 5.0.3

  • Include the output of dotnet --info image

  • The IDE - VS 2019 16.8.6

fingers10 avatar Mar 02 '21 14:03 fingers10

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Mar 02 '21 17:03 ghost

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.

ghost avatar Jul 20 '21 17:07 ghost

Hello @fingers10. As you mentioned from the docs:

Data binding works with a single DateTime format string using @bind:format="{FORMAT STRING}", where the {FORMAT STRING} placeholder is the format string. Other format expressions, such as currency or number formats, aren't available at this time but might be added in a future release.

This is not currently planned, but we'll use this issue to track community interest for this feature.

TanayParikh avatar Oct 19 '21 05:10 TanayParikh

My company is keen for bind format string support against numeric types, we currently have to do the formatting ourselves whenever we want specific precision on our floating point values. Being able to bind directly to the double with a format string would be great, especially since Blazor / the browser would then handle the localisation for us :)

stewart-james avatar May 12 '22 15:05 stewart-james

<input type="number" @bind-value="@number" @bind-value:format="0.000000" />

Error (active) CS1503 Argument 1: cannot convert from 'double' to 'System.DateTime' ...

Very bad.

harveytriana avatar Nov 12 '24 18:11 harveytriana