Asp.net mvc ASP.NET核心中的货币格式问题
在带有Asp.net mvc ASP.NET核心中的货币格式问题,asp.net-mvc,visual-studio-2015,asp.net-core,asp.net-core-mvc,Asp.net Mvc,Visual Studio 2015,Asp.net Core,Asp.net Core Mvc,在带有EF Core 1.1的我的ASP.NET Core 1.1中,下面的视图显示的是成本值,不带美元格式。我想把它显示为,比如,$1945.25问题:我可能缺少什么注意:我试过并发布了,但仍然是相同的问题 型号: public class costs { .... .... [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")] public float? Costs { get; s
EF Core 1.1
的我的ASP.NET Core 1.1
中,下面的视图
显示的是成本
值,不带美元格式。我想把它显示为,比如,$1945.25
问题:我可能缺少什么注意:我试过并发布了
,但仍然是相同的问题
型号:
public class costs
{
....
....
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
public class TestViewModel
{
....
....
public string ProdName{ get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
@model myProj.Models.TestVewModel
....
....
<td>@Html.DisplayFor(t =>t.Cost)</td>
<td>@Html.EditorFor(t =>t.Cost)</td>
....
....
TestViewModel:
public class costs
{
....
....
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
public class TestViewModel
{
....
....
public string ProdName{ get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
@model myProj.Models.TestVewModel
....
....
<td>@Html.DisplayFor(t =>t.Cost)</td>
<td>@Html.EditorFor(t =>t.Cost)</td>
....
....
查看:
public class costs
{
....
....
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
public class TestViewModel
{
....
....
public string ProdName{ get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
@model myProj.Models.TestVewModel
....
....
<td>@Html.DisplayFor(t =>t.Cost)</td>
<td>@Html.EditorFor(t =>t.Cost)</td>
....
....
@model myProj.Models.TestVewModel
....
....
@DisplayFor(t=>t.Cost)
@EditorFor(t=>t.Cost)
....
....
更新:
public class costs
{
....
....
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
public class TestViewModel
{
....
....
public string ProdName{ get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
@model myProj.Models.TestVewModel
....
....
<td>@Html.DisplayFor(t =>t.Cost)</td>
<td>@Html.EditorFor(t =>t.Cost)</td>
....
....
我能想到的post和one之间的唯一区别可能是,我使用的SQL Server 2012的数据类型为
real
forcosts
列。但是,我不确定这是否是问题的原因。real
数据类型是通过EF-Core-migration
命令创建的。如果什么都不起作用,您可以选择其中一个:
<td>$@t.Cost</td>
$@t.Cost
或
@string.Format(“{0:C}”,t.Cost)
首先,不要使用float
作为货币。使用十进制
。像.1这样简单的东西如果不使用近似逻辑就无法准确表示。但是,那不是你的问题。我不能完全确定你的问题是什么。。只是它的格式不正确,即使在使用DisplayFormat属性和DisplayFor/EditorFor时也是如此吗?查看您的代码,您正在将属性放置在costs
对象上,但您正在视图中使用TestVewModel
。我知道你说,TestViewModel
与上面的一样,但它真的是这样吗?为什么不给我们一个小的可验证样本。仅供参考,请参阅-这表明值的格式正确。所以很明显,您正在做一些您没有向我们展示的事情,这是在破坏它。@ErikFunkenbusch问题只与数据显示格式不正确有关。值显示正确,但与数字一样,例如1945.25
而不是$1945.25
,我也尝试了DisplayFormat属性和DisplayFor/EditorFor。我刚刚添加了TestViewModel
code和一个更新部分(如果有帮助的话)。另外,将float
更改为decimal
会对实际值产生任何影响吗?正如我所说,float不是您的问题,但您确实不想将float用于货币,因为浮点舍入错误。正如您在我链接到的dotnetfiddle中所看到的,使用该属性格式化效果很好。它与sql server类型无关。