MVC4 Editor Template and DisplayFormat Attribute Not Working?

Posted on 2013-11-16
Medium Priority
Last Modified: 2013-11-21
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

    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 = ""; }

        <td>@Html.TextBoxFor(x => x.Amount, new { style="width:75px" })</td>
Question by:WorknHardr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 52

Accepted Solution

Carl Tawn earned 1600 total points
ID: 39655793
Try using @Html.EditorFor instead of @Html.TextBoxFor

Apparently TextBoxFor doesn't honour the DataFormatString attribute, whereas EditorFor does.

Author Comment

ID: 39662457
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.

Author Closing Comment

ID: 39667198
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question