【问题标题】:DataAnnotations: DisplayFormat not being honoredDataAnnotations:DisplayFormat 不被尊重
【发布时间】:2017-03-18 18:43:40
【问题描述】:

这两个我都试过了:

[Display(Name = "Contract Value")]
[DisplayFormat(DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }

还有这个:

[Display(Name = "Contract Value")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }

查看:

@Html.LabelFor(x => contractInfoModel.ContractValue)
@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "100" })

如果我这样做,它会起作用。但我不想这样做:

<input type="text" name="@Html.NameFor(x=>contractInfoModel.ContractValue)" id="@Html.IdFor(x=>contractInfoModel.ContractValue)" value="@Model.ContractInfoM.ContractValue.ToString("0.##")" class="numeric" maxlength="10" />

显示名称效果很好。但它仍然显示 4 个小数位。我做错了什么?

谢谢!

【问题讨论】:

  • 尝试用零替换井号。我不确定它是否有效,但请尝试一下。
  • 不!值得一试,但没有
  • 是的,我已经看到了。这也不起作用:[DisplayFormat(DataFormatString="{0:#.####}")]

标签: c# asp.net-mvc data-annotations


【解决方案1】:

找到了:

根据设计,TextBoxFor 中不支持 DisplayFormat。所以不要这样:

@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

我这样做了:

@Html.EditorFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

而且效果很好。

【讨论】:

  • 如果你想使用TextBoxFor()它的@Html.TextBoxFor(x =&gt; contractInfoModel.ContractValue, "{0.##}", new { maxlength = "10" })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多