在使用MVC显示模板格式化货币 [英] Formatting currency using display template in MVC
问题描述
我发现这个职位有关显示和EditorTemplates为MVC:
I found this post about Display and EditorTemplates for MVC:
<一个href=\"http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html\">http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html
它创建了一个显示模板轻松地显示与货币符号格式化小数。
It creates a display template to easily display a decimal formatted with currency sign.
在本例中使用的模型:
public class TestModel
{
public decimal Money { get; set; }
}
显示模板:
查看/共享/ DisplayTemplates / decimal.cshtml:
Views/Shared/DisplayTemplates/decimal.cshtml:
@model decimal
@{
IFormatProvider formatProvider =
new System.Globalization.CultureInfo("en-US");
<span class="currency">@Model.ToString("C", formatProvider)</span>
}
在我的网站我有一个辅助类的方法来检索十进制格式化货币字符串,所以我会替换上面的东西,如:
In my website I have a helper class with a method to retrieve a formatted currency string from a decimal, so I would replace the above with something like:
@model decimal
@(MyHelperClass.GetCurrencyString(Model))
最后,我们希望看到的格式的货币的观点:
And finally the view where we want to see the formatted currency:
@model TestModel
@Html.DisplayFor(e => e.Money)
输出:
<span class="currency">$3.50</span>
我可以毫无问题实现这一点。但我当然有我想要查看格式的货币不同的看法。但在某些情况下,我不希望显示的货币符号。
I can implement this without any problem. But of course i have different views where i want to view a formatted currency. But in some cases I don't want to show the currency sign.
我现在的问题是我应该如何实现这个小小的变化,而不在code多的矫枉过正。
My question now is how i should implement this small variation without to much overkill in code.
下面是我目前的执行情况:
Here is my current implementation:
我已经改变了我的显示模板这样:
I've changed my display template to this:
@model decimal
@{
bool woCurrency = (bool)ViewData["woCurrency"];
}
@(MyHelperClass.GetCurrencyString(Model)Model,woCurrency))
当然我也改为GetCurrencyString方法来接受这种附加属性。
Of course i've also changed to GetCurrencyString method to accept this additional attribute.
在我看来,我现在有太多提供此属性:
In my view I now have to provide this attribute too:
@Html.DisplayFor(m => m.Money, new { woCurrency = true })
所以,其实我的一切工作像它应该工作。但不知何故,我不喜欢这个sollution,使视图更复杂。
So actually I everything works like it should work. But somehow I don't like this sollution that makes the view more complex.
我对你的问题:有没有实施这样的事情任何其他方法?或者有什么意见,可以优化我当前sollution?
My question to you: Is there any other method to implement something like this? Or any advice to possible optimise my current sollution?
谢谢!
推荐答案
如何的HtmlHelper
来检查计算机[woCurrency]
自动输出正确的结果?
How about HtmlHelper
that checks the ViewData["woCurrency"]
automatically and outputs the correct result?
public static string Currency(this HtmlHelper helper, decimal data, string locale = "en-US", bool woCurrency = false)
{
var culture = new System.Globalization.CultureInfo(locale);
if (woCurrency || (helper.ViewData["woCurrency"] != null && (bool)helper.ViewData["woCurrency"]))
return data.ToString(culture);
return data.ToString("C", culture);
}
然后:
@Html.Currency(Model.Money);
这篇关于在使用MVC显示模板格式化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!