Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:14:18+00:00 2026-06-02T23:14:18+00:00

I have a model with a number I’d like to format in a specific

  • 0

I have a model with a number I’d like to format in a specific way when it is displayed in my view.

[DisplayFormat(DataFormatString = "{### ##}")] 
 [Display(Name = "Postnr")] 
 public string CustomerZip;

in the view:

@Html.DisplayFor(modelItem => item.CustomerZip) 

In the DB the values are stored as ##### so I get an error when trying this approach: “Input string was not in a correct format”. I thought (or rather hoped) that the DataFormatString would reformat the string for me.

Any suggestions on how to do this the best way is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:14:19+00:00Added an answer on June 2, 2026 at 11:14 pm

    A few things:

    1. The DataFormatString needs to use the {0:…} indexer style format string, like you’d use in string.Format. So you’d need to do something like [DisplayFormat(DataFormatString="{0:### ##}"]. If the braces are supposed to be literal, then you’d use [DisplayFormat(DataFormatString="{{{0:### ##}}}")]. However…
    2. There is no such format character # for strings. That is what you’d use for a numeric type. If you are storing the value as an integer, then you’re in luck; just change the type of CustomerZip to int.
    3. If you are NOT storing the value as an integer, but as a string (as you are wont to do with postal codes, usually), then you’ve got more of a problem. Like I said, you don’t have any custom format options with strings. You’d need to represent CustomerZip as some custom type that is IFormattable, which you would define.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say I have such model: class Foo(models.Model): name = models.CharField(name,max_length=25) type = models.IntegerField(type number)
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
Say I have django model that looks something like this: class Order(models.Model): number =
I have model Products with columns: name, number, description Index defined for this model
I have a model that has an arbitrary number of children entities. For simplicity
Let's say I have a QTableView with a corresponding model. This view shows numbers
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
Let's have a model public class Model { public int Number { get; set;
Let's see if I can explain myself, I have this models: class BillHeader(models.Model): number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.