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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With