有没有一种方法可以使用 View 模型属性上的 DisplayFormat
属性来为社会安全号码或电话号码应用 DataFormatString
格式?我知道我可以使用 javascript 来做到这一点,但如果可能的话,我更愿意让模型来处理它。
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "???????")]
public string Ssn { get; set; }
最佳答案
以下应该可以工作,但请注意 Ssn 属性的类型差异。
[DisplayFormat(DataFormatString = "{0:###-###-####}")]
public long Phone { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###-##-####}")]
public long Ssn { get; set; }
请注意,为了应用格式,您需要在 View 中使用以下 html 帮助器:
@Html.DisplayFor(m => m.Property)
关于asp.net-mvc - 电话号码或社会保险号的 DisplayFormat.DataFormatString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981049/
相关文章:
java - 是否可以在 Eclipse 中将部分 java 代码标记为不自动格式化?
r - 如何在不丢失行名和列名的情况下对 R 中的平面列联表进行子集化?
formatting - 引用 YAML(对于 Travis CI)
formatting - 使用 LaTeX Beamer 将引文文本放在同一张幻灯片上
.net - 为什么 DateTime.ToShortTimeString() 不尊重 "Regio
eclipse - 如何配置 Eclipse 格式化程序在字段声明之间不插入空行?
python - 设置 ipython 的默认科学记数法阈值