12

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; }
AJ.
  • 16,368
  • 20
  • 95
  • 150

2 Answers2

15

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)
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • 2
    Hmm, I'm going to mess around with this, but both Phone and SSN are stored as VARCHAR in my database, so I'll have to do a little footwork to get those properties set up as `long`. There's no way to do it with the properties typed as `string`? – AJ. Jun 11 '12 at 14:26
  • 2
    One option, would be to setup a view model with properties of type long and the related data annotations and simply convert the type between your domain entity and view model: Convert.ToInt64("123456789") – Jesse Jun 11 '12 at 15:08
  • 2
    lol...love downvotes on answers that are over a year old with no corresponding comments.... – Jesse Nov 06 '13 at 17:37
  • I didn't down-vote, but I can tell you that in 2014 on VS2013, using .NET 4.0.2 it just does not work :( I tried, textboxfor, editorfor, custom templates, long type, on property, string type. NOTHING! so I will just skip the "fanciness" of MVC Frx, and use old tested JQuery techniques :) – Pawel Cioch Jul 28 '14 at 15:17
  • @PawelCioch what is **.Net 4.0.2** ? – T-moty Apr 04 '16 at 15:35
  • 6
    I did down vote because as a DBA, storing phone and SSN as a Long data type is, well, just incorrect. Problem is, a phone (international) or SSN, which has a leading 0, will be lost. Those are significant digits. They must stay fixed in the "number. Int, Long, etc should only be used for "calculated' numbers. There may be exceptions, but SSN and Phone are not exceptions. This is the same explanation as an answer below. – SnapJag May 30 '17 at 05:53
  • What if I want to display the phone number in a textbox like this? @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control" }) – mcfred Dec 23 '17 at 20:04
3

The accepted answer is great if the type is an integer, but a lot of ids wind up being typed as a string to prevent losing leading zeros. You can format a string by breaking it up into pieces with Substring and make it reusable by sticking it in a DisplayTemplate.

Inside /Shared/DisplayTemplates/, add a template named Phone.vbhtml:

@ModelType String
@If Not IsNothing(Model) AndAlso Model.Length = 10 Then
    @<span>@String.Format("({0}) {1}-{2}",
              Model.Substring(0, 3),
              Model.Substring(3, 3),
              Model.Substring(6, 4))</span>
Else
    @Model
End If

You can invoke this in a couple ways:

  1. Just annotate the property on your model with a data type of the same name:

    <DataType("Phone")> _
    Public Property Phone As String
    

    And then call using a simple DisplayFor:

     @Html.DisplayFor(Function(model) model.Phone)
    
  2. Alternatively, you can specify the DisplayTemplate you'd like to use by name:

     @Html.DisplayFor(Function(model) model.VimsOrg.Phone, "Phone")
    
KyleMit
  • 30,350
  • 66
  • 462
  • 664