Ryg*_*guy 33 string.format razor asp.net-mvc-3
我正在尝试格式化Html.EditorFor文本框以进行货币格式化,我试图将它基于TextBoxFor上的此线程String.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) })
Run Code Online (Sandbox Code Playgroud)
我正在做的是代码,这里是编辑器字段div中包含的网站中该字段的html.
<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">
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢!
Dar*_*rov 71
您可以GoalAmount使用以下[DisplayFormat]属性修饰视图模型属性:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal GoalAmount { get; set; }
Run Code Online (Sandbox Code Playgroud)
在视图中简单地说:
@Html.EditorFor(model => model.Project.GoalAmount)
Run Code Online (Sandbox Code Playgroud)
EditorFor帮助器的第二个参数完全不符合您的想法.它允许您将其他ViewData传递给编辑器模板,它不是htmlAttributes.
另一种可能性是为currency(~/Views/Shared/EditorTemplates/Currency.cshtml)编写自定义编辑器模板:
@Html.TextBox(
"",
string.Format("{0:c}", ViewData.Model),
new { @class = "text-box single-line" }
)
Run Code Online (Sandbox Code Playgroud)
然后:
@Html.EditorFor(model => model.Project.GoalAmount, "Currency")
Run Code Online (Sandbox Code Playgroud)
或使用[UIHint]:
[UIHint("Currency")]
public decimal GoalAmount { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后:
@Html.EditorFor(model => model.Project.GoalAmount)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70270 次 |
| 最近记录: |