我有一个带有数字的模型,当它显示在我的视图中时,我想以特定方式对其进行格式化。
[DisplayFormat(DataFormatString = "{### ##}")]
[Display(Name = "Postnr")]
public string CustomerZip;
在视图中:
@Html.DisplayFor(modelItem => item.CustomerZip)
在数据库中,值存储为#####,因此在尝试这种方法时出现错误:“输入字符串的格式不正确”。我认为(或者更确切地说是希望)DataFormatString 会为我重新格式化字符串。
任何有关如何以最佳方式做到这一点的建议都值得赞赏。