Sam*_*ong 2 c# asp.net asp.net-mvc
我直截了当地说到这一点.
我试图显示Monthly Income只有2位小数.
我尝试过使用DisplayFormat但是当我在Textbox上添加它时它不起作用.
模型
public class AgentModel
{
[Display(Name = "Monthly Income")]
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal MonthlyIncome { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图
//this input display the two decimal
@Html.EditorFor(m => m.MonthlyIncome, new { htmlAttributes = new { @class = "form-control" } })
//this one display 5 decimal
@Html.TextBoxFor(m => m.MonthlyIncome, new { @class = "form-control"})
Run Code Online (Sandbox Code Playgroud)
我很困惑这两个输入之间有什么区别.我正在使用,DataFormat因为我希望格式集中在我的模型上.并且不使用此代码@string.Format("{0:N2}",decimal.Round(agent.MonthlyIncome, 2, MidpointRounding.AwayFromZero))来限制小数位.因为如果我这样做,我会在所有观点中这样做.
我也尝试输出值 monthly income
<td>@agent.MonthlyIncome</td>
Run Code Online (Sandbox Code Playgroud)
这仍然返回5位小数.
小智 6
要使用显示格式化值TextBoxFor(),请使用此重载
@Html.TextBoxFor(m => m.MonthlyIncome, "{0:0.00}", new { @class = "form-control"})
Run Code Online (Sandbox Code Playgroud)
第二个参数是格式字符串.请注意,DisplayFormatAttribute仅在使用EditorFor()或时才会遵守DisplayFor()
另请注意,<td>@Html.DisplayFor(m => m.MonthlyIncome)</td>将使用正确的格式呈现值.
| 归档时间: |
|
| 查看次数: |
1856 次 |
| 最近记录: |