gpt4 book ai didi

c# - C# 中用于强类型 View 的字符串格式化

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:21 24 4
gpt4 key购买 nike

我想知道如何将强类型 View 价格字段转换为 2 位数字说明符,就像我在我的数据库中有一个货币字段,例如将 15 转换为 15.0000,我只想在 View 中显示 15.00,下面是代码:

<%: Html.TextBoxFor(model =>model.Price, new { maxlength = "5", style = "width:40px;" })%>

我尝试了类似的方法但没有成功:

<%: Html.TextBoxFor(model => String.Format("{0:n}"model.Price), new { maxlength = "5", style = "width:40px;" })%>

最佳答案

您可以在模型上放置一个属性,例如:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n}")]

如果您不想这样做,那么您将需要使用“旧”样式的文本框:

<%= Html.TextBox("Price", string.Format("{0:n}", Model.Price)) %>

关于c# - C# 中用于强类型 View 的字符串格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com