Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayFormat.DataFormatString for a phone number or social security number

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; }
like image 348
AJ. Avatar asked Jun 11 '12 13:06

AJ.


1 Answers

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)
like image 72
Jesse Avatar answered Oct 01 '22 10:10

Jesse