DisplayFormat.DataFormatString一个电话号码或社会安全号码 [英] DisplayFormat.DataFormatString for a phone number or social security number

查看:116
本文介绍了DisplayFormat.DataFormatString一个电话号码或社会安全号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,我可以使用 DisplayFormat 属性在视图模型属性来应用 DataFormatString 格式社会安全号码或电话号码?我知道我可以用JavaScript这样做,但将preFER有模型来处理它,如果可能的话。

  [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString =???????)]
公共字符串SSN {搞定;组; }


解决方案

下面应该工作,但要注意的SSN属性的类型不同。

  [DisplayFormat(DataFormatString ={0:###  -  ###  -  ####})]
众长电话{搞定;组; }[DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:### - ## - ####})]
众长SSN {搞定;组; }

请注意,即为了要应用的格式,你需要使用下面的HTML帮助在您的视图:

  @ Html.DisplayFor(M = GT; m.Property)

Is there a way I can use the DisplayFormat attribute on a view model property to apply a DataFormatString format for a social security number or a phone number? I know I could do this with javascript, but would prefer to have the model handle it, if possible.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "???????")]
public string Ssn { get; set; }

解决方案

The following should work, however notice the type difference for the Ssn property.

[DisplayFormat(DataFormatString = "{0:###-###-####}")]
public long Phone { get; set; }

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###-##-####}")]
public long Ssn { get; set; }

Note, that in order for the formatting to be applied you would need to use the following html helper in your view:

@Html.DisplayFor(m => m.Property)

这篇关于DisplayFormat.DataFormatString一个电话号码或社会安全号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆