Reputation: 65
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?
Upvotes: 2
Views: 4291
Reputation: 31
@Html.Raw(@String.Format("{0:c}", @Model.YourFieldName))
Posting this because I couldn't find this answer anywhere. This ended up working for me.
Upvotes: 3
Reputation: 5284
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")
you can get more information form this and this link.
Upvotes: 3