Last active
April 7, 2020 14:30
-
-
Save ncarandini/8b5165d2a2c51591b81f20e70241f6b4 to your computer and use it in GitHub Desktop.
Use a string property to edit decimal value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() | |
{ | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very nitty-gritty solution that don't require a custom validation or component. Not intended for production use, only for demonstrating a concept.