我的MVC应用程序允许ADM用户动态更改十进制数的小数位。 我需要更改显示格式,所以我编写了以下代码:
public ForecastProfitView(decimal? literPerUnit = null, int? _decimalPlaces = null)
{
LiterPerUnit = ((literPerUnit ?? 0) == 0) ? 1 : literPerUnit.Value;
decimalPlaces = ((_decimalPlaces ?? 0) == 0) ? 2 : _decimalPlaces.Value;
}
private decimal LiterPerUnit { get; }
private static int decimalPlaces { get; set; }
[Display(ResourceType = typeof(Language.App_GlobalResources.AnalysisAndManagement), Name = "BottlingMaterialsCost")]
[DecimalDynamicDisplayFormat(decimalPlaces)]
public decimal BottlingMaterialsCost { get; set; }
当我设置[DecimalDynamicDisplayFormat(decimalPlaces)]
时,它会给我一个错误,因为我需要一个常量表达式。有没有办法解决这个问题?
答案 0 :(得分:1)
没有。你只限于编译时常量。基本上所有可以是const
的东西。
我将格式保存在一个单独的属性中,因为它是动态的,并执行以下操作:
@Html.TextBoxFor(m => m.BottlingMaterialsCost, bottlingMaterialsCostFormat)
@Html.TextBoxFor(m => m.BottlingMaterialsCost, "{0:0.00}")
<强>更新即可。您可以执行以下操作来计算格式:
var bottlingMaterialsCostFormat = ((_decimalPlaces ?? 0) == 0) ? "{0:00}" : "{0:" + new string('0', _decimalPlaces.Value) + "}";