user603007
user603007

Reputation: 11794

how to display dollar format for decimals in razor?

I am trying to display a dollar format for my razor list view. It displays decimals fe 100.17 but I am looking for $100.17 format in my list:

@foreach (var item in Model) {
<tr>
    <td>
      @Html.DisplayFor(modelItem => item.Amount))
   </td>
</tr>

I got a gutfeeling I need some kind of htmlextension for this but could not find this. Any other ways of doing this?

Upvotes: 0

Views: 2203

Answers (1)

Martin Booth
Martin Booth

Reputation: 8595

You can decorate the property (Amount) in the model itself like this:

[DisplayFormat(DataFormatString = "{0:c}")]
public decimal Amount { get; set; }

Upvotes: 2

Related Questions