Copied to clipboard

Flag this post as spam?

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


  • Richard Galt 64 posts 230 karma points
    Jul 09, 2014 @ 17:03
    Richard Galt
    0

    Listing media items

    Hi,

    I'm very new to razor and struggling with something.

    I have a partial view which outputs related links in the left nav, I'm trying to do the same thing with related media e.g. pdf's that I've selected with the multiple media picker. This is the code for the links list:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @*
        Related Items / Links / Pages 
    *@
    
    @{
        if ( Model.Content.HasValue("relatedItems") )
        {
            if ( CurrentPage.relatedItems.Count > 0 )
            {
                <div class="related-items">
                    <div class="page-header">
                        <h2>@umbraco.library.GetDictionaryItem("Related")</h2>
                    </div>
                    <ul class="nav nav-list">
                        @foreach(var link in CurrentPage.relatedItems)
                        {
                            <li>
                                @{
                                    var linkTarget = link.newwindow == "1" ? "target=\"_blank\"" : "";
                                }
                                <a href="@link.link" @Html.Raw(linkTarget)>@link.title</a>
                            </li>
                        }
                    </ul>
                </div>
            } 
    
        }
    }

     

    Can anybody help with this and also suggest where I can find some good reference material to learn Razor? Not having intellisense and syntax checking in the back office is a real pain :(

    Thanks

    Rich

  • Dan Lister 416 posts 1974 karma points c-trib
    Jul 10, 2014 @ 11:22
    Dan Lister
    1

    Hi Rich,

    If you install Jeavon Leopold's Umbraco Core Property Value Converters package and follow his documentation, you'll be able to write something like the following:

    @{
        var media = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("relatedItems");
        if (media.Any())
        {
            foreach (var item in media)
            {
                <a href="@item.Url" target="_blank">@item.Name</a>
            }
        }
    }
    

    Thanks, Dan.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 10, 2014 @ 21:29
    Jeavon Leopold
    0

    Hi Rich,

    While of course I would agree with Dan that my package makes this super simple you can also do it without and there is a code snippet in the documention here

    Also if you want intellisence you can used the typed model (Model.Content) and Visual Studio Express or WebMatrix to edit your Views/Partials/Etc

    Jeavon

  • Richard Galt 64 posts 230 karma points
    Jul 11, 2014 @ 14:46
    Richard Galt
    0

    Thank you Dan and Jeavon.

    I have re worked the code as per the example but it doesn't appear to be rendering?

     

     @if (Model.Content.HasValue("relatedMedias"))
    {
            if ( CurrentPage.relatedMedias.Count > 0 )
           {
               <div class="related-links">
                    <div class="page-header">
                       <h2>@umbraco.library.GetDictionaryItem("RelatedMedia")</h2>
                   </div>
                   <ul class="nav nav-list">
             @{
                var relatedMediasList = Model.Content.GetPropertyValue<string>("relatedMedias").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var relatedMediasCollection = Umbraco.TypedMedia(relatedMediasList).Where(x => x != null);
           }
                           @foreach (var relatedMedia in relatedMediasCollection)
                           {
                           <li>
                               <img src="@relatedMedia.Url" style="width:300px;height:300px" />
                           </li>
                           }
                       </ul>
       </div>
       }
           } 
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 11, 2014 @ 22:35
    Jeavon Leopold
    100

    Hi Richard,

    Please try,

     @if (Model.Content.HasValue("relatedMedias"))
    {             
                var relatedMediasList = Model.Content.GetPropertyValue<string>("relatedMedias").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var relatedMediasCollection = Umbraco.TypedMedia(relatedMediasList).Where(x => x != null);
    
            if ( relatedMediasCollection.Any() )
           {
               <div class="related-links">
                    <div class="page-header">
                       <h2>@Umbraco.GetDictionaryValue("RelatedMedia")</h2>
                   </div>
                   <ul class="nav nav-list">
                           @foreach (var relatedMedia in relatedMediasCollection)
                           {
                           <li>
                               <img src="@relatedMedia.Url" style="width:300px;height:300px" />
                           </li>
                           }
                       </ul>
       </div>
       }
           } 
    
  • Richard Galt 64 posts 230 karma points
    Jul 14, 2014 @ 14:29
    Richard Galt
    0

    Thanks Jeavon that works a treat :)

Please Sign in or register to post replies

Write your reply to:

Draft