[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register 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
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

656 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