Copied to clipboard

Flag this post as spam?

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


  • Seth Hoveland 3 posts 83 karma points
    Jan 27, 2017 @ 16:18
    Seth Hoveland
    0

    Razor - getting media URL in a roundabout way?

    I am stuck. Just getting started with MVC and Umbraco at the same time, and I can't suss out how to make this happen or if I am going about it in the wrong way. Any help is appreciated.

    I am trying to make a simple list of posts including link, title, media, date and summary.

    What I have so far:

    @{
    var articles = Umbraco.Content(1216);  //Node of Posts
    
        foreach (var article in articles.Children.Where("Visible")) //Getting visible posts
        {
            <a href="@article.Url">
                <div class="col-md-6">
                    <h5>@article.Name</h5>
                    <div><img src="/media/@article.Image/SOMETHING"/></div> <!--Gets the node of the correct media item, not sure how to get the actual file name with only the node -->
                    <p>@article.postDate</p>
                    <p>@article.summary</p>
                </div>
            </a>
        }
    

    }

    How do I get the actual image URL associated with the article, only knowing the node id? Or is there a better way to do what I am attempting? Side request, what is the best way to format the date (show only date without time). Thanks!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2017 @ 16:33
    Dennis Aaen
    100

    Hi Seth and welcome to our.

    You can do something like this.

    @{      
        if (article.HasValue("propertyAlias")){                                         
            var dynamicMediaItem = Umbraco.Media(article.propertyAlias);
     <div>
            <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
    </div>
        }
    }
    

    Please remember to change propertyAlias so it match your field alias where you are adding your image.

    Hope this helps,

    /Dennis

  • 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