guys. I would like to ask if there's anyway to make an int input to currency format? I have tried using
[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
public int TotalAmount { get; set; }
but, it would show an empty
@Html.EditorFor
And if I used
[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public int TotalAmount { get; set; }
It would only show 0.00
What I would like is when I input 10000, it would automatically format to
Rp. 10.000
inside the @Html.EditorFor field.
Any suggestions?
Try this:
"C" or "c" Currency Result:
[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
public decimal TotalAmount { get; set; }
OR
[DisplayFormat(DataFormatString = "{0:C0}")]
public decimal TotalAmount { get; set; }
"N" or "n" Number Result:
[DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
public decimal TotalAmount { get; set; }
In Entity Framework
[DataType(DataType.Currency)]
public decimal TotalAmount { get; set; }
EditorTemplates Example:
Create a editor template for currency (~/Views/Shared/EditorTemplates/Currency.cshtml)
:
Currency.cshtml
@Html.TextBox("", string.Format("{0:c}", ViewData.Model),new { @class = "text-box single-line" })
Use:
@Html.EditorFor(model => model.TotalAmount, "Currency")