DisplayFormat.DataFormatString for a phone number or social security number

asp.net-mvc, asp.net-mvc-4, data-annotations, formatting

Solution

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)

Problem

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; } ```

Original source