是否可以为DataType为DataType.Currency的MVC3字段强制使用货币 [英] Is it possible to force the currency for an MVC3 field with DataType as DataType.Currency

查看:163
本文介绍了是否可以为DataType为DataType.Currency的MVC3字段强制使用货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个MVC3应用程序,该应用程序从数据库中读取一堆货币数据.我的问题是这些金额全部使用不同的货币.

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;}

我得到小数位和一个货币符号,看起来不错,但默认为用户的本地货币.美国用户看到$423.29,而GB用户看到£423.29.我可以在Web.config中使用<globalization culture="{something}">覆盖货币,但这会全局设置所有货币字段.

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; }

,并且始终将其渲染为$439.38,但是内置注释无法实现.

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

推荐答案

我这样做的方法是创建一个扩展DataType属性的自定义属性和一个自定义html帮助器.这不一定是最简单的方法,但将来可以节省时间.

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.

编辑 合并了CultureInfo.CreateSpecificCulture(cultureName)而不是switch

自定义属性

public class CurrencyDisplayAttribute : DataTypeAttribute
{
    public string Culture { get; set; }

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

HTML帮助器

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.

这篇关于是否可以为DataType为DataType.Currency的MVC3字段强制使用货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆