Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben 112 posts 398 karma points
    Dec 23, 2015 @ 19:25
    Ben
    0

    Razor html attribute assignment in Umbraco v7.3.4

    In Umbraco v7.2.8 I was able to use the following code without problems:

        foreach(var myPerson in foundPeople){
    var facultyStaffImage = Library.MediaById(6870);
    
                    if(myPerson.HasValue("facultyStaffPhoto")){
                        facultyStaffImage = Library.MediaById(myPerson.facultyStaffPhoto);
                    }
          <a href="@myPerson.Url"><img src="@facultyStaffImage.umbracoFile" class="facultyImageSmall" /></a>
          <br />
          <a href="tel:1555444@myPerson.phoneExtension">(555) 444-@myPerson.phoneExtension</a> 
          <br />
        }
    

    which creates an href value of something like tel:15554443333

    In Umbraco v7.3.4 this creates an href value of something like tel:1555444@myPerson.phoneExtension

    To fix this I had to change to code to look like this:

        foreach(var myPerson in foundPeople){
    var facultyStaffImage = Library.MediaById(6870);
    
                    if(myPerson.HasValue("facultyStaffPhoto")){
                        facultyStaffImage = Library.MediaById(myPerson.facultyStaffPhoto);
                    }
          var htmlPhoneLine = "<a href='tel:1585594" + myPerson.phoneExtension + "'>(585) 594-" + myPerson.phoneExtension + "</a>";
    
          <a href="@myPerson.Url"><img src="@facultyStaffImage.umbracoFile" class="facultyImageSmall" /></a>
          <br />
          @Html.Raw(htmlPhoneLine)
          <br />
        }
    

    The href value of the anchor tag around the image still works though.

    Is this the new way to do it, or is there a better way to assign the attributes of html elements in Razor?

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Dec 24, 2015 @ 08:18
    Marc Goodson
    1

    Hi Ben

     <br />
          <a href="@("tel:1555444" + myPerson.phoneExtension)">(555) 444-@myPerson.phoneExtension</a> 
          <br />
    

    should also work

    ie performing the concatenation inside the @() syntax

    It's breaking in your example because the @myPerson.phoneExtension is in the middle of the attribute and razor is not parsing it...

    regards

    marc

  • gary 385 posts 916 karma points
    Dec 24, 2015 @ 08:25
    gary
    0

    Hi Ben, Marc

    Just noticed something . .

    var htmlPhoneLine = "<a href='tel:1585594" + myPerson.phoneExtension + "'>(585) 594-" + myPerson.phoneExtension + "</a>";
    

    In front of the tel: it is not a ".

    Maybe it will work if you amend this.

    Shouldn't be any changes between 7.2.8 and current, that is what made me look closer.

    Regards

    Gary

  • Ben 112 posts 398 karma points
    Dec 24, 2015 @ 13:51
    Ben
    100

    Hi Gary,

    Sorry I didn't provide the complete file. I have "@{}" around the whole code and since the that line is with in the foreach loop it didn't need the @ infront of it. It is currently working.

    In v7.2.8 I was able to write out a line like this and it worked:

    <a href="tel:1555444@myPerson.phoneExtension">(555) 444-@myPerson.phoneExtension</a>
    

    Something seems to have changed between v7.3.0 and v7.3.4.

    I will try the following when I get a chance:

    <a href="@("tel:1555444" + myPerson.phoneExtension)">(555) 444-@myPerson.phoneExtension</a>
    

    Thanks for your help Marc and Gary! I really appreciate it!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies