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>
WorknHardrAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.