有没有一种方法可以使用 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)
我是一名优秀的程序员,十分优秀!