如何为MVC EditorFor设置货币格式? [英] How to set up currency format for MVC EditorFor?

查看:112
本文介绍了如何为MVC EditorFor设置货币格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须转到需要以货币格式显示项目金额的页面.第一个是输入货币的地方,第二个是显示货币的地方.

I have to pages that need to show the item amount with currency format. The first one is where currency is entered and the second one where currency is displayed.

我希望EditorFor显示R表示的兰德,然后我希望该值是十进制.

I would like the EditorFor to show an R indicated Rands and then i would like the value to be decimal.

这是我的EditorFor:

Here is my EditorFor:

<div class="editor-field">
    @Html.EditorFor(model => model.TransactionModel.Price)
</div>

我尝试了许多不同的方法,却无济于事.

I have tried many different ways and can't get any to work.

在下面的示例中,它不知道第二行中的"CurrencyPrice".

With this example below it does not know what 'CurrencyPrice' is in the second line.

var CurrencyPrice = '@Model.TransactionModel.Price';
document.getElementById('TransactionModel.Price').value = '@string.Format("{0:C}", CurrencyPrice)'; 

我也在我的transactionModel中尝试过这些:

I have also tried these in my transactionModel:

//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
//[UIHint("Currency")]
//[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
//[DataType(DataType.Currency)]
public decimal Price { get; set; }

有人可以告诉我如何正确处理吗?

Could someone please tell me how i can get this right?

现在我将接受任何可行的方法.

Right now i will accept any working method.

推荐答案

对于MVC 4,您可以使用包含格式字符串的TextBoxFor方法:

With MVC 4, you can use the TextBoxFor method that includes a format string:

@Html.TextBoxFor(model => model.TransactionModel.Price, "{0:c}")

在MVC 3和更低版本中:

In MVC 3 and below:

@Html.TextBoxFor(model => model.TransactionModel.Price, 
    new { @Value = model.TransactionModel.Price.ToString("c") })

EditorFor可能会或可能不会工作.就个人而言,我遇到了问题.有关更多灵感,请参见此答案.

EditorFor might or might not work. Personally, I have had problems with it. For more inspiration, see this answer.

这篇关于如何为MVC EditorFor设置货币格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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