• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1157
  • Last Modified:

MVC4 Editor Template and DisplayFormat Attribute Not Working?

I'm using MVC4 and Editor Templates. The TextBoxFor will not return the correct format of two decimal places: 28.71
Instead it returns: 28.7100
Help!

[Model]
    public class ExpenseEditViewModel
    {
        public DateTime Date { get; set; }
        public List<Expensee> Expenses { get; set; }
    }

    public class Expensee
    {
        [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
        public decimal Amount { get; set; }
    }

[Editor Template]
@model BudgetProjectUI.Expensee
@{ Layout = ""; }

<table>
    <tr>              
        <td>@Html.TextBoxFor(x => x.Amount, new { style="width:75px" })</td>
   </tr>
</table>
0
WorknHardr
Asked:
WorknHardr
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Try using @Html.EditorFor instead of @Html.TextBoxFor

Apparently TextBoxFor doesn't honour the DataFormatString attribute, whereas EditorFor does.
0
 
WorknHardrAuthor Commented:
I recently switched to TextBoxFor because EditorFor doesn't work well with css. Maybe I should just trim the trailing zeros I don't want.
0
 
WorknHardrAuthor Commented:
I recently switched to TextBoxFor because EditorFor doesn't work well with css. Maybe I should just trim the trailing zeros I don't want.

thx for helping
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now