Copied to clipboard

Flag this post as spam?

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


  • J 447 posts 864 karma points
    Mar 23, 2016 @ 11:42
    J
    0

    Razor help to embed image

    I have the below code which is using Javascript and Razor. The below code is within the Javascript parameter which is working

    foreach (var childPage in Model.Children.Where("Visible"))
    {
        var CustomerAndImage = @childPage.CustomerName + '<img src="' + @Model.MediaById(@childPage.image).umbracoFile +'" alt="Porcelain Factory of Vista Alegre" height="115" width="83">';               
        <text>['@CustomerAndImage'],</text>
    }
    

    I'm trying to embed an image to be part of the variable CustomerAndImage but whenever i add 'Too many characters in character literal"

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 23, 2016 @ 11:50
    Dave Woestenborghs
    0

    Hi J,

    This because you are single quotes. In c# (Razor) this can only contain one character. For full strings you need to surround it with double quotes :

    foreach (var childPage in Model.Children.Where("Visible"))
    {
        var CustomerAndImage = @childPage.CustomerName + "<img src=\"" + @Model.MediaById(@childPage.image).umbracoFile +"\" alt=\"Porcelain Factory of Vista Alegre\" height=\"115\" width="83">";               
        <text>['@Description'],</text>
    }
    

    Dave

  • J 447 posts 864 karma points
    Mar 23, 2016 @ 12:06
    J
    0

    Hi Dave - thanks

    I did that, added and an extra \ where it was missed off but instead of the img tag being embedded its just displaying the code as text? Any idea?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 23, 2016 @ 12:20
    Dave Woestenborghs
    0

    Can you post your entire code. This is just a few lines. Can not see where it is going wrong

  • J 447 posts 864 karma points
    Mar 23, 2016 @ 12:28
    J
    0

    I've changed some of it whilst refactoring. Please see below

    @inherits umbraco.MacroEngines.DynamicNodeContext

    <script src="http://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    
    var MapLocations = [
    @if (Model.Children.Where("Visible").Any())
    {
        var naviLevel = Model.Children.Where("Visible").First().Level;
    
            foreach (var childPage in Model.Children.Where("Visible"))
            {
                var LatLng = @childPage.LatLng;
                var Customer = @childPage.CustomerName;
                var Image = "<img src=\"" + @Model.MediaById(@childPage.image).umbracoFile + "\" alt=\"test\" height=\"115\" width=\"83\">";
    
                <text>['@Customer' + '@Image', @LatLng],</text>
            }
     }
        ];
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 23, 2016 @ 12:44
    Dave Woestenborghs
    100

    Hi J,

    Can you do this :

     <text>['@Customer' + '@Html.Raw(Image)', @LatLng],</text>
    

    Dave

  • 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