[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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