Copied to clipboard

Flag this post as spam?

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


  • Mads Sørensen 188 posts 433 karma points
    Mar 12, 2015 @ 09:48
    Mads Sørensen
    0

    Change variable value in partial view

    Hi guys
    I'm messing around with a super simple script.

    I got a variable with a fallback value, but when i try to change the value with a value from a mediaPicker property it gives me an error. I've done this kind of changing 100 of times but i just cant figure this one out?!?

    var image = "/images/02.jpg";
                
        if(Model.Content.HasValue("topImage"))
        {
            image = Umbraco.Media(CurrentPage.topImage);
        } 


    Error message:

    Cannot implicitly convert type 'Umbraco.Web.Models.DynamicPublishedContent' to 'string'

    Line 12:         image = Umbraco.Media(CurrentPage.topImage);

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 12, 2015 @ 10:03
    Dennis Aaen
    0

    Hi Mads,

    Could you perhaps use this approach. So if the media picker doesn't have a value then you take the default image.

    if (Model.Content.HasValue("topImage")){                                         
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("topImage"));
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>   
     }else{
            <img src="/images/02.jpg" alt="02"/>
     }

    or

     var image = "/images/02.jpg";
     
     if (Model.Content.HasValue("topImage")){                                        
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("topImage"));
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>   
     }else{
            <img src="@image" alt="02"/>
     }

    Hope this helps,

    /Dennis

  • Mads Sørensen 188 posts 433 karma points
    Mar 12, 2015 @ 10:10
    Mads Sørensen
    0

    Hi Dennis
    Thanks for reply - but the problem is that the value is used as an inline property for a div.

    I think the problem is that I start with an variable as a text string and then change it to Umbraco value - or something like that :P

    Maybe there is a better practice for this problem :D

        var size = "s";
        var image = "/images/02.jpg";
    
        if(Model.Content.HasValue("topImage"))
        {
             image = Umbraco.Media(Model.Content.GetPropertyValue("topImage"));
        }
    
        if (Model.Content.Where("DocumentTypeAlias = \"Forsiden\""))
        {
            size = "l";
        }
    
    
            <div id="page-header" class="padding-@size full-image" data-image-src="@image" data-parallax="scroll" data-ios-fix="true" data-android-fix="true" data-speed="0.05">

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 12, 2015 @ 10:14
    Dennis Aaen
    0

    Hi Mads,

    I that that the problem is that when you are picking an image with the media picker you just get the id of the item in the media section, e.g 1058. So as you said the variable started as a text string and then change it to an integer to get the id of the picture that you are choosen with the media picker.

    /Dennis

  • Mads Sørensen 188 posts 433 karma points
    Mar 12, 2015 @ 10:19
    Mads Sørensen
    0

    Well i know that is should look like this :D

      if(Model.Content.HasValue("topImage"))
        {
            image = Umbraco.TypedMedia(Model.Content.GetPropertyValue("topImage")).umbracoFile;
        }
Please Sign in or register to post replies

Write your reply to:

Draft