37

I'm trying to format an Html.EditorFor textbox to have currency formatting, I am trying to base it off of this thread String.Format for currency on a TextBoxFor. However, my text just still shows up as 0.00 with no currency formatting.

<div class="editor-field">
        @Html.EditorFor(model => model.Project.GoalAmount, new { @class = "editor-     field", Value = String.Format("{0:C}", Model.Project.GoalAmount) })

There is the code for what I am doing, and here is the html for that field in the website itself contained within the editor-field div of course.

<input class="text-box single-line valid" data-val="true" 
 data-val-number="The field Goal Amount must be a number." 
 data-val-required="The Goal Amount field is required."
 id="Project_GoalAmount" name="Project.GoalAmount" type="text" value="0.00">

Any help would be appreciated, thanks!

Community
  • 1
  • 1
Him_Jalpert
  • 2,476
  • 9
  • 31
  • 55

1 Answers1

75

You could decorate your GoalAmount view model property with the [DisplayFormat] attribute:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal GoalAmount { get; set; }

and in the view simply:

@Html.EditorFor(model => model.Project.GoalAmount)

The second argument of the EditorFor helper doesn't do at all what you think it does. It allows you to pass additional ViewData to the editor template, it's not htmlAttributes.

Another possibility is to write a custom editor template for currency (~/Views/Shared/EditorTemplates/Currency.cshtml):

@Html.TextBox(
    "", 
    string.Format("{0:c}", ViewData.Model),
    new { @class = "text-box single-line" }
)

and then:

@Html.EditorFor(model => model.Project.GoalAmount, "Currency")

or use [UIHint]:

[UIHint("Currency")]
public decimal GoalAmount { get; set; }

and then:

@Html.EditorFor(model => model.Project.GoalAmount)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • What is the format string for Currency (pounds) without pence? I cant seem to find it as of yet. Thanks. – IAmGroot Feb 04 '13 at 14:41
  • @DarinDimitrov I have now! ;) Thanks. – IAmGroot Feb 04 '13 at 16:13
  • 1
    I had to cast your `ViewData.Model` to `decimal` after converting or parsing it to one for the `TextBox` to satisfy the correct overload. `string.Format("{0:c}", (decimal)decimal.Parse(Model))) )` – The Muffin Man Feb 07 '13 at 04:54
  • 2
    How do you get around the problem that a formatted currency field causes with the model binding when the form is posted? Since there are invalid characters for a decimal in the form field, the binding doesn't fill the value when the form is posted. Seems heavy handed to have to create a custom binder for this scenario... – Mark Feb 08 '13 at 13:50
  • `Seems heavy handed to have to create a custom binder for this scenario` - That's precisely what I would do if I had to support this scenario. But I would write it in such a way so that it uses the `DisplayFormat` attribute with which was decorated the view model property and reuse the same format for displaying and binding. – Darin Dimitrov Feb 08 '13 at 13:54
  • This helped me but I set `ApplyFormatInEditMode = true` to `ApplyFormatInEditMode = false` - This stops the validation message of input must be a number, when currency symbol is present in edit text box. - I think the user should know what currency he is working with - any way if its multiple currency management, the currency symbol should be stored with the value, otherwise how would one know if the amount is in Pounds, Dollars or Euros if looking at the database. – Piotr Kula May 08 '14 at 20:41
  • Glad to see this question has helped out quite a few people! – Him_Jalpert Jul 03 '14 at 16:59
  • I needed to add `@model IFormattable` at the beginning. Also these days we can write typesafe `[DataType(DataType.Currency)]` instead of `UIHint` – Christian Gollhardt Jan 17 '16 at 11:05