Copied to clipboard

Flag this post as spam?

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


  • Iulia Maria Jensen 40 posts 71 karma points
    Apr 08, 2014 @ 11:37
    Iulia Maria Jensen
    0

    Get Media item when @model is in use

    Hi,

    I am trying to write a Partial View in Umbraco 7.1 to render a box element (has a doctype, a node and properties) but I am running into problems when trying to render my Image which is filled in on the box through a Media Picker.

    I can't really access it even though I tried different techniques I found in different posts around and about.

    Any help would be appreciated.

    This is what I have so far:

    @using Newtonsoft.Json
    @model IPublishedContent
    @{
        UmbracoHelper _helper = new UmbracoHelper(UmbracoContext.Current);
        dynamic bottomLink = JsonConvert.DeserializeObject(@Model.GetPropertyValue<string>("link"));
        var imgID = @Model.GetPropertyValue("grafik");
        var img = "";
    }
    <div class="box box-spot">
        @imgID
        
    </div>

     

    @imgID returns an ID if I just write it out but when I try to use it, it looks like "imgID" is actually an obj which I can't really just convert to a string or int.

    Thanks.

    Iulia

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 08, 2014 @ 11:47
    Jeavon Leopold
    1

    Hi Lulia,

    For the image you should be doing it like this:

    @inherits UmbracoViewPage<IPublishedContent>
    @{
        if (Model.HasValue("grafik"))
        {
            var mediaItem = Umbraco.TypedMedia(Model.GetPropertyValue("grafik"));
            <div class="box box-spot">
                <img src="@mediaItem.Url" alt="@mediaItem.GetPropertyValue("Name")" />
            </div>
        }
    }
    

    But what property editor is your data type using for you "link" property?

    Jeavon

  • Iulia Maria Jensen 40 posts 71 karma points
    Apr 08, 2014 @ 15:50
    Iulia Maria Jensen
    0

    Thanks, Jeavon!

    That really helps!

    Well, I am fairly new to Umbraco 7 and I was missing a Url Picker functionality. But then I found "Multi Url Picker" made by Rasmus John which I started using.

    Aside from having to manually add a "ng-app" in the angularjs code since without it, it failed, it works just fine... Now I am not sure if that's a change really worth mentioning since as said, I am new to Umbraco 7 so I am not convinced yet as to why it was missing.. Luckily I am not that new to angular :). Anyway, as you can see in the code, the expected output is JSON and that in return gets rendered on the page. I think it's a really nice and nifty package.

    If I would have had enough points, I would have given the package a vote ;).

    Iulia

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 08, 2014 @ 16:30
    Jeavon Leopold
    0

    Hi Lulia

    Great, I hadn't even heard of that package! It seems a bit similar to the built in related links editor though.

    For your reference all of the MVC documentation is located here and the specific section about strongly typed partial views (like you have) is here

    Jeavon

  • Iulia Maria Jensen 40 posts 71 karma points
    Apr 09, 2014 @ 08:31
    Iulia Maria Jensen
    0

    Hi Jeavon,

    Yes, I suppose you're right. The related links editor is very similar to this package. It seems though as if you can't choose Media items with it? I may be wrong though. I will check it out.

    And many thanks for the links..those should help me get started :-).


    Kind regards,

    Iulia

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 09, 2014 @ 10:45
    Jeavon Leopold
    0

    Hi Lulia,

    Yes you are right the core related links editor doesn't currently allow the selection of media and also you can't currently reorder the items.

    It would be nice if the multi url picker shipped with a converter so you didn't have deserialize the JSON every time you used it, I will have a look and see if it's up for contribution :-)

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft