出现FormatException上Html.DisplayFor为十进制值

问题描述:

我的模型具有DisplayFormat设置如图图像下面出现FormatException上Html.DisplayFor为十进制值

[BsonElement("spread")] 
[BsonRepresentation(BsonType.Decimal128,AllowTruncation =true)] 
[DisplayFormat(DataFormatString = "{N6}")] 
public decimal Spread { get; set; } 

在我看来,我有以下代码:

<tbody> 
    @for (int i = 0; i < Model.Entries.Count; i++) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(item => item.Entries[i].Spread) 
      </td> 

然而,当页面负载,我得到的错误:

FormatException: Input string was not in a correct format. System.Text.StringBuilder.FormatError() System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, string format, ParamsArray args) string.FormatHelper(IFormatProvider provider, string format, ParamsArray args) Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TemplateBuilder.Build() Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GenerateDisplay(ModelExplorer modelExplorer, string htmlFieldName, string templateName, object additionalViewData) Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperDisplayExtensions.DisplayFor(IHtmlHelper htmlHelper, Expression> expression) AspNetCore._Views_Dashboard_Bordao_cshtml+d__30.MoveNext() in Bordao.cshtml + @Html.DisplayFor(item => item.Entries[i].Spread)

我试图改变formatString"0.000""N6" ..仍然没有成功.. 如果在查看代码之外运行string.format(spread, "N6"),我会得到正确的结果。

如何获得我的视图来格式化小数点后6位小数。 感谢

+1

与6位小数工作,但是,根据截图,错误似乎是从'Spread'属性来 – adiga

+0

您的代码应该工作正常,没有任何问题关于您应用于'InitialBalance'属性的格式。你确定这是问题的原因吗?看截图,它是一个不同的属性!也不要发布代码图片,将相关部分代码本身发布到问题中。 – Shyju

+0

抱歉图像发布。 Spread属性的配置方式相同。 –

仍然不知道为什么N6不起作用。得到它使用

[DisplayFormat(DataFormatString = "{0:0.#######}")] 

[DisplayFormat(DataFormatString = "{0:N6}")]