Copied to clipboard

Flag this post as spam?

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


  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 18:06
    Danny Penrose
    0

    Getting media url using ID from property needs Integer or string

    Hi,

    I'm using the following in a Macro and it works great however when i use it in my Master template, or any other template i get an error.

    var mediaId = Umbraco.Field("heroImage");
    var media = Umbraco.Media(mediaId);
    
    <img src="@media.Url">
    

    Exception Details: System.InvalidOperationException: The value of parameter 'id' must be either a string or an integer

    But it is an Integer and works fine in macros and mediaId is giving me the media Id!- i must be missing something really obvious!

    TIA

  • jivan thapa 194 posts 681 karma points
    Sep 06, 2015 @ 18:28
    jivan thapa
    0

    You may get this error if mediaId is empty. Make sure that mediaId is not null or empty.

    Have you try like this one.

     var mediaId = Umbraco.Field("heroImage");
        if (!string.IsNullOrEmpty(mediaId.ToString()))
        {
    
        var media = Umbraco.Media(mediaId);
            if (media != null)
            {
                <img src="@media.Url">
            }
        }
    
  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 18:37
    Danny Penrose
    0

    Thanks for the reply Jivan, the mediaId is not null

    If i do:

    var mediaId = Umbraco.Field("heroImage");
    @mediaId
    

    I get

    1125
    

    If i do this it works:

    var media = Umbraco.Media(1125);
    

    So confused!

  • jivan thapa 194 posts 681 karma points
    Sep 06, 2015 @ 18:44
    jivan thapa
    100

    Hum

    what if you parse IHtmlString to string and get the media.

     var mediaId = Umbraco.Field("heroImage");
        var media = Umbraco.Media(mediaId.ToString());
    
  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 18:46
    Danny Penrose
    0

    Bingo - thanks very much for your help.

    Please can you explain why that was needed in Template but not in Macro?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 06, 2015 @ 19:01
    Nik
    0

    Hey Danny,

    Have you tried checking what your line:

    var mediaId = Umbraco.Field("heroImage");
    

    results in?

    Look at the value of mediaId first, then also check that the field heroImage exists. If mediaId is null then that would explain your issue.

    Personally, I would use CurrentPage instead of Umbraco to access the property, I'd also wrap the code in a check to ensure the property exists in the first place.

  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 19:06
    Danny Penrose
    0

    Thanks Nik, as above mediaId = 1125

    That's why i don't understand why i needed toString when an int should work too!

    I'll be wrapping it in a conditional for sure - will explore CurrentPage aswell.

  • jivan thapa 194 posts 681 karma points
    Sep 06, 2015 @ 19:18
    jivan thapa
    0

    Not sure why, but It might be that Umbraco.Field returns IHtmlString type so parsing it to string or integer, it always uses strong type instead of object.

  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 19:38
    Danny Penrose
    0

    Spot on Jvan, it does indeed look like Umbraco.Field returns IHtmlString - it was working in my macro because i was using MacroParameters not Umbraco.Field, had to come away from the screen for 5 before i realised!

    Thanks again for the help.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 06, 2015 @ 20:23
    Dennis Aaen
    0

    Hi Danny,

    Try to see this overview https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/ of the different property editors, that you find in Umbraco.

    In this documentation you will get the Razor snippet for how to get data from the different property editors. In both version the dynamic version and the strongly typed version.

    Hope this helps,

    /Dennis

  • Danny Penrose 7 posts 37 karma points
    Sep 06, 2015 @ 21:39
    Danny Penrose
    0

    Perfect, many thanks Dennis.

  • Andre Roussakoff 13 posts 74 karma points
    Dec 05, 2015 @ 17:53
    Andre Roussakoff
    0

    Hi there, I have had the same problem. Running in VS2015 debugger, I noticed that mediaId has indeed the "Id" property Id (capital "I"), which has an int value. The exception on the other hand is talking about the "id" property (small "i"). Could this difference explain the reason why the exception is being thrown in the first place? Greetz, Andre

Please Sign in or register to post replies

Write your reply to:

Draft