MVC4 Editor Template and DisplayFormat Attribute Not Working?

Posted on 2013-11-16
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 400 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 122
Gridview alignment 1 19
Where did System.Data.Objects go? 2 23
Expression Evaluater 3 24
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

806 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