Copied to clipboard

Flag this post as spam?

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


  • Sebastian Patten 10 posts 40 karma points
    Aug 29, 2018 @ 13:50
    Sebastian Patten
    0

    Better way to check for Field value on an image

    var image = Umbraco.Media(Umbraco.Field("myImage").ToString());
    var lang = "en";
    var fieldValue = image.GetPropertyValue<string>($"alt_{lang}");
    

    I have the above code. I see that image is of type dynamic. But with dynamics I'm never sure whether a property or method exists. Is there a more strongly typed/better way of doing the above?

    Ultimately I'd like to:

    • For a given page
    • Get the image from the field
    • Get some field value from the image
  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Aug 29, 2018 @ 14:06
    Dave Woestenborghs
    100

    Hi Sebastian,

    To get a strongly typed media object change your first line of code to this :

    var image = Umbraco.TypedMedia(Umbraco.Field("myImage").ToString());
    

    Dave

  • Sebastian Patten 10 posts 40 karma points
    Aug 29, 2018 @ 15:03
    Sebastian Patten
    0

    Hi fives - thanks Dave

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Aug 29, 2018 @ 14:07
    Dave Woestenborghs
    0

    But if you really like to work with strongly typed models you can have a look at ModelsBuilder

    I wrote a article that should get you started ; https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/

    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