In my view I have the following call.
<%= Html.EditorFor(x => x.Cost) %>
I have a ViewModel with the following code to define Cost.
public decimal Cost { get; set; }
However this displays a decimal value with four digits after the decimal (e.g. 0.0000). I am aware of Decimal.toString("G")
(MSDN) which appears to solve this issue, but I'm uncertain of where to apply it.
One solution seems to be create a partial view "Currency.aspx".
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Decimal>" %>
<%= Html.TextBox(Model.ToString("g"), new { @class = "currency" }) %>
And a [UIHint("Currency")]
in my ViewModel.
This seems inelegant. I assume that this problem has been solved tidily somewhere in the MVC framework or C# but I am unaware of cleaner solutions.
What is the appropriate way to handle editing currency values in MVC?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…