0
在我的ASP.NET Core 1.1
與EF Core 1.1
之後,View
顯示Cost
價值沒有美元格式。我想將其顯示爲$1,945.25
。 問題:我可能會丟失什麼? 注意:我試過this和thisSO Posts
但仍然是同樣的問題。ASP.NET CORE中的貨幣格式問題
型號:
public class costs
{
....
....
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public float? Costs { get; set; }
....
....
}
TestViewModel:
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>
....
....
UPDATE:
我能想到的this職位和this一個唯一的區別可能是我使用的是SQL Server 2012中具有的數據類型爲real
列costs
。但是,我不確定這是否是問題的原因。 real
數據類型是通過EF-Core migration
命令創建的。
嗯,首先,不使用貨幣'float'。使用'decimal'。如果不使用近似邏輯,就不能準確地表達.1這樣的簡單內容。但是,那不是你的問題。我不完全確定你的問題是什麼。是否它的格式不正確,即使使用DisplayFormat屬性和DisplayFor/EditorFor?看着你的代碼,你將你的Attribute放在'costs'對象上,但是你在你的視圖中使用了'TestVewModel'。我知道你說'TestViewModel'是「與上面相同」,但它是真的嗎?爲什麼不給我們一個小的可驗證的樣本。 –
僅供參考,請參閱https://dotnetfiddle.net/2cGikc - 這顯示正確格式化的值。很明顯,你正在做一些你沒有向我們展示的東西。 –
@ErikFunkenbusch問題只與數據顯示格式不正確有關。值正確顯示,但正如像'1945.25'而不是'$ 1,945.25'這樣的數字,我也嘗試過DisplayFormat屬性和DisplayFor/EditorFor。如果有幫助,我剛剛添加了'TestViewModel'代碼和** UPDATE **部分。另外,如果將'float'更改爲'decimal',實際值會有什麼不同? – nam