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 8532759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:47:19+00:00 2026-06-11T09:47:19+00:00

I’m writing an MVC3 application that reads in a bunch of monetary data from

  • 0

I’m writing an MVC3 application that reads in a bunch of monetary data from a database. The issue I have is that these amounts are all in different currencies.

If I set the type of a field like this:

[DataType(DataType.Currency)]
public Amount{ get; set;}

I get the decimal places and a currency symbol, which looks nice, but it defaults to the user’s local currency. A US user sees $423.29 whereas a GB user sees £423.29. I can override the currency by using a <globalization culture="{something}"> in the Web.config, but this sets all currency fields globally.

What would be the easiest way of marking up a field so that it renders with the correct decimal places and currency symbol?

In an ideal world, I’d like to be able to do something like this (for USD):

[DataType(DataType.Currency, culture="en-us")]
public Amount{ get; set; }

and have that always render as $439.38, but that’s not possible with the built-in annotations.

  • 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-11T09:47:20+00:00Added an answer on June 11, 2026 at 9:47 am

    The way I would do this is to create a custom attribute that extends the DataType attribute and a custom html helper. It’s not necessarily the easiest way of doing it but it would save time in the future.

    EDIT
    Incorporated CultureInfo.CreateSpecificCulture(cultureName) instead of a switch

    Custom Attribute

    public class CurrencyDisplayAttribute : DataTypeAttribute
    {
        public string Culture { get; set; }
    
        public CurrencyDisplayAttribute(string culture)
            : base(DataType.Currency)
        {
            Culture = culture;
        }
    }  
    

    Html Helper

    public static class Helpers
    {
        public static IHtmlString CurrencyDisplayFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            double value = double.Parse(expression.Compile().Invoke(helper.ViewData.Model).ToString());
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    
            var prop = typeof (TModel).GetProperty(metadata.PropertyName);
    
            var attribute = prop.GetCustomAttribute(typeof (CurrencyDisplayAttribute)) as CurrencyDisplayAttribute;
    
            // this should be whatever html element you want to create
            TagBuilder tagBuilder = new TagBuilder("span");
            tagBuilder.SetInnerText(value.ToString("c", CultureInfo.CreateSpecificCulture(attribute.Culture));
    
            return MvcHtmlString.Create(tagBuilder.ToString());
        }
    }
    

    You can use the attribute in your model

    [CurrencyDisplay("en-us")]
    public double Amount { get; set; }
    

    Then in your view you can use the helper by

    @Html.CurrencyDisplayFor(x => x.Amount);
    

    Provided your model is passed in correctly.

    Obviously, you’d need to do error checking and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.