Link to home
Start Free TrialLog in
Avatar of lovettjay
lovettjay

asked on

MVC5 Dataformat String for Phone not working

I have tried several different things and have had no luck


In my member class, so I know it's a string.  
public string members_phone { get; set; }

Open in new window


In the metadata folder
[Required(ErrorMessage = "Phone is required")]
		[Display(Name = "Phone")]
		[DisplayFormat(DataFormatString = "{0:(###)###-####}", ApplyFormatInEditMode = true)]
		[RegularExpression(@"^[0-9]{0,15}$", ErrorMessage = "Phone Number should contain only numbers -  Example: 8008008000")]
		[StringLength(15)]
		public string members_phone { get; set; }

Open in new window


Also in the view I have tried a few different things:

@String.Format("{0:(###) ###-####}", Model.members_phone)

Open in new window

@String.Format("{0:(###) ###-####}", Html.DisplayFor(model => model.members_phone))

Open in new window

@Convert.ToString(String.Format("{0:(###) ###-####}", Model.members_phone))

Open in new window



The only time I can get it to work it when I hard code a phone number
@String.Format("{0:(###) ###-####}", 8008008000)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lovettjay
lovettjay

ASKER

This is pulling from the DB.  Do you have an example that I could look at of how to store as int, or long?
Thank you for your help