Close

how to format text string as phone number

joster
11 months ago
#14823 Quote
Avatar
  • 127
Hello-

I am using the short description field to store the phone number for each of my locations, and I have modified the shop.cshtml to display the short description value just below the location title.

I want to link the number with a tel hyperlink so that mobile device users can click the number to bring up the dial pad....
<a href="tel:@Html.Raw(Model.ShortDescription)">@Html.Raw(Model.ShortDescription)</a>


..but when the values I have stored in the database are formatted like this:
(123) 456-7890
The user touches the number on his device and the parentheses, space, and hyphen are converted to "7", so the number is not correct to dial...

So I changed my stored values to have only digits, like: 1234567890
But it looks bad to visitors...

So I wrote this code on my Shop.cshtml to format the value for the visitor to better understand:

@{
string thephone="";
thephone=@Html.Raw(Model.ShortDescription);

<span style="position:relative;left:20px;top:5px;text-align:left;font-size:22px;font-weight:bold;color:green;">
<a href="tel:@string.Format("{0: (###) ###-####}",@thephone)">@Html.Raw(Model.ShortDescription)</a></span>
}


But I always get an error:
h:\root\home\myaccount\www\nopSite\Plugins\SevenSpikes.Nop.Plugins.StoreLocator\Views\StoreLocator\Shop.cshtml(41): error CS0029: Cannot implicitly convert type 'System.Web.IHtmlString' to 'string'

I tried other variations, but it's always the same error. I really am just guessing...

The solution does not have to be in C# MVC....I think maybe just a jQuery or Javascript can do it because I only want to affect the output to the screen..not form input..but I don't know.

Can somebody please help me?
anton_ivanov
11 months ago
#14824 Quote
Avatar
  • Moderator
  • 264
Hello,

As the error suggests you the HtmlString and String are not compatible types. You will have to convert the result from @Html.Raw(Model.ShortDescription) to string before assigning it to the thephone variable, like this:

thephone=@Html.Raw(Model.ShortDescription).ToString();

Hope that helps!
Regards,
Anton Ivanov
Nop-Templates.com