Skip to content

Instantly share code, notes, and snippets.

@ncarandini
Last active April 7, 2020 14:30
Show Gist options
  • Save ncarandini/8b5165d2a2c51591b81f20e70241f6b4 to your computer and use it in GitHub Desktop.
Save ncarandini/8b5165d2a2c51591b81f20e70241f6b4 to your computer and use it in GitHub Desktop.
Use a string property to edit decimal value
@page "/EditTicket"
@using System.Globalization
<h3>Edit Ticket # @Ticket.Id</h3>
<EditForm Model="Ticket" OnSubmit="Save">
<InputText @bind-Value="Ticket.Title" />
<InputNumber @bind-Value="Ticket.Price" />
<InputDate @bind-Value="Ticket.Date" />
<button class="btn btn-primary">Save</button>
<div class="form-group">
<label for="Price">Price:</label>
<InputText id="Price" @bind-Value="PriceAsString" class="form-control" />
</div>
</EditForm>
@code {
Ticket Ticket = new Ticket
{
Id = Guid.NewGuid(),
Title = "Fight FCO-SEA",
Price = 836M,
Date = DateTime.Now
};
string PriceAsString
{
get { return Ticket.Price.ToString("#,##0.00", CultureInfo.CurrentCulture); }
set
{
if (Decimal.TryParse(value, NumberStyles.Number,
CultureInfo.CurrentCulture,out decimal result))
{
Ticket.Price = result;
}
}
}
private void Save()
{
// ...
}
}
@ncarandini
Copy link
Author

A very nitty-gritty solution that don't require a custom validation or component. Not intended for production use, only for demonstrating a concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment