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

问题描述

我试图格式化文本框Html.EditorFor有货币格式,我想它的基础了这个线程的String.Format对于在TextBoxFor 货币。然而,我的文字刚才还显示为0.00,没有货币格式。

I'm trying to format an Html.EditorFor textbox to have currency formatting, I am trying to base it off of this thread String.Format for currency on a TextBoxFor. However, my text just still shows up as 0.00 with no currency formatting.

<div class="editor-field">
        @Html.EditorFor(model => model.Project.GoalAmount, new { @class = "editor-     field", Value = String.Format("{0:C}", Model.Project.GoalAmount) })

有是code为我做什么,这里是本身当然包含的编辑场专区内的网站领域的HTML。

There is the code for what I am doing, and here is the html for that field in the website itself contained within the editor-field div of course.

<input class="text-box single-line valid" data-val="true"
 data-val-number="The field Goal Amount must be a number."
 data-val-required="The Goal Amount field is required."
 id="Project_GoalAmount" name="Project.GoalAmount" type="text" value="0.00">

任何帮助将是AP preciated,谢谢!

Any help would be appreciated, thanks!

推荐答案

您可以装饰你的 GoalAmount 视图模型属性 [DisplayFormat] 属性:

You could decorate your GoalAmount view model property with the [DisplayFormat] attribute:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal GoalAmount { get; set; }

和在视图中简单:

@Html.EditorFor(model => model.Project.GoalAmount)

在EditorFor助手的第二个参数根本不去做你认为它。它允许你传递额外的ViewData给编辑模板,它不是htmlAttributes。

The second argument of the EditorFor helper doesn't do at all what you think it does. It allows you to pass additional ViewData to the editor template, it's not htmlAttributes.

另一种可能性是写货币自定义编辑器模板(〜/查看/共享/ EditorTemplates / Currency.cshtml

Another possibility is to write a custom editor template for currency (~/Views/Shared/EditorTemplates/Currency.cshtml):

@Html.TextBox(
    "",
    string.Format("{0:c}", ViewData.Model),
    new { @class = "text-box single-line" }
)

和则:

@Html.EditorFor(model => model.Project.GoalAmount, "Currency")

或使用 [UIHint]

[UIHint("Currency")]
public decimal GoalAmount { get; set; }

和则:

@Html.EditorFor(model => model.Project.GoalAmount)

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

08-06 05:51