English 中文(简体)
MVC 货币格式化
原标题:Currency Formatting MVC

我试图格式化 Html. Editor For for textbox 格式化货币格式, 我正试图将其基于 < a href=> https://stackoverflow.com/ questions/69637497string-for-moden-on- a- textboxfor > > String。 TextBoxfor 上的货币格式Format。 然而, 我的文本仍然显示为0.00, 没有货币格式 。

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

网站本身的这个域的 html 包含在编辑 - 字段Div里 当然。

<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">

任何帮助都会感激不尽,谢谢!

最佳回答

您可以以 [DisplayFormat] 属性来设置您的 goalAmount 查看模型属性:

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

并且简单地认为:

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

编辑器“ 助手” 的第二个参数完全不做您认为它所做的一切。 它允许您将额外的 ViewData 传送到编辑器模板, 它不是 htmlAttrittes 。

另一种可能性是为货币写一个自定义编辑模板():

@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)
问题回答

暂无回答




相关问题
Why do the overloads of String.Format exist?

I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 arguments were optimized ...

How can I determine if a composite format string is invalid?

Per the documentation, String.Format will throw a FormatException if either (A) the format string is invalid or (B) the format string contains an index that cannot be found in the args array. I want ...

How do I format Int32 numbers?

What is the best way to get formatted Int32 numbers? Let say I have this o function: string o(int x); This is the value that o need to return according to x x = 0 => o = 00 x = 1 => ...

what does this string.format piece of code do?

I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter stwr, bool getColumnName) { for (int i = 0; i < reader.FieldCount; i++) stwr....

format string- compile time checking

Is there any way to check the format string at compile time ? Example: Console.WriteLine("{0} is a really {1} site", "stackoverflow.com", "cool");//this will run //this will give an exception as ...

Sticking HTML formatting into System.String object in C#

How do I stick HTML formatting into a String object in C#? Here s what I have: c.DepartmentAbbr.ToString() + " - (" + c.DepartmentName.ToString() + ")" where c.DepartmentAbbr.ToString() and c....