Copied to clipboard

Flag this post as spam?

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


  • riski 11 posts 101 karma points
    Jan 03, 2018 @ 05:19
    riski
    0

    How get image path from @Umbraco.Field

    Hi..

    How get image path from @Umbraco.Field .. i have create image insert form in content and won display in my custom template "partial view".. i have try but i just get image id like src="2311".

    can anyone help me.

    thanks

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jan 03, 2018 @ 09:07
    Alex Skrypnyk
    1

    Hi Riski

    Number is id of media item, you have to get url by additional method

    Try this code:

    Umbraco.Media(Umbraco.Field("test")).Url
    

    Thanks,

    Alex

  • riski 11 posts 101 karma points
    Jan 04, 2018 @ 10:50
    riski
    0

    thanks, i use this in .cshtml template but not work and my web display blank page

    this the code :

    var imgID = Umbraco.Media(Umbraco.Field("cartImage")).url; @imgID

    i have try to other tricks like use 2 variable but still not work. but if i use like this : src="Umbraco.Media(@Umbraco.Field("cartImage")).url" and the result is :

    src="Umbraco.Media("2311").url"

    thanks

  • Rune Hem Strand 147 posts 911 karma points hq c-trib
    Jan 04, 2018 @ 12:25
    Rune Hem Strand
    1

    Hi Riski

    I think you are starting the razor statement in the wrong place :) Try doing this:

     src="@Umbraco.Media(Umbraco.Field("cartImage")).url"
    

    All the best
    Rune

  • riski 11 posts 101 karma points
    Jan 05, 2018 @ 03:12
    riski
    0

    Thank for your reply, i don't know why but after i use your solution my web still give blank page, this my full code :

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits UmbracoViewPage<dynamic>
    @using Umbraco.Web.Templates
    @using Newtonsoft.Json.Linq
    
    @if (Model != null && Model.sections != null)
    {
        var oneColumn = ((System.Collections.ICollection)Model.sections).Count == 1;
    
        <div class="umb-grid">  
            @if (oneColumn)
            {
                foreach (var section in Model.sections) 
                {
    
                        <div class="grid-section"> 
    
    
                        @foreach (var row in section.rows) 
                        {
    
    
                                @renderRow(row, true);
    
    
    
                        }
    
                        <section id="footer-area" class="sc"></section>
    
                    </div>
                }   
            }
            else 
            { 
                <div class="container">
                    <div class="row clearfix">
                        @foreach (var s in Model.sections) 
                        {
                            <div class="grid-section">
                                <div class="[email protected] column">
                                    @foreach (var row in s.rows) 
                                    {
                                        @renderRow(row, false);
                                    } 
    
                                </div>
                            </div>
    
                        }
                    </div>
                </div>   
            }
    
        </div>
    }
    
    @helper renderRow(dynamic row, bool singleColumn) 
    {
            if(row.name == "masthead"){ 
                <div id="@row.id" class="sc masthead scroll-img unavigation campaign" @RenderElementAttributes(row)>                
                @foreach ( var area in row.areas ) {
    
    
                        <div class="container right col-md-5"> 
    
                        @foreach (var control in area.controls) {  
                            if(control.value == "itembutton"){
                                <div style="display: none;"><img id='&#105;&#109;&#103;@Umbraco.Field("dataClass")' src="@Umbraco.Media(Umbraco.Field("cartImage")).url" alt="" rel="1948" data-id="1948"></div>
                                <div id="@Umbraco.Field("dataClass")"> 
                                <p> 
                                <a id="@Umbraco.Field("dataId")" 
                                    data-id="@Umbraco.Field("dataId")"  
                                    data-gav="@Umbraco.Field("dataGAID")"
                                    data-class="@Umbraco.Field("dataClass")"
                                    data-discqty="0"
                                    data-discprice="0"
                                    data-qty="@Umbraco.Field("itemQTY")"
                                    data-price="@Umbraco.Field("itemPrice")"
                                    data-desc="@Umbraco.Field("dataDesc")"
                                    data-name="@Umbraco.Field("itemName")"  
                                    href="#" class="@Umbraco.Field("dataClass") add-to-list btn btn-outline">Add to Cart</a> 
                                </p>
                                </div>
                            }else if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            } 
                        }
                        </div>
                    }
    
                <div id="[email protected]" class="scroll-wrap"></div> 
    
                </div>
                    <div class="sc cd-secondary-nav unscroll-img unnavigation sticky" id="@Umbraco.Field("itemSKU")"> 
                                <div class="container"> 
                                                <ul class="nav navbar-nav navbar-left">
                                                <li><img id="&#105;&#109;&#103;@Umbraco.Field("dataClass")" src="/media/1309/5-thumbnail.jpg" alt="" rel="1500" data-id="1500" /></li>
                                                <li>
                                                <h4>test product 123123"</h4>
                                                </li>
                                                </ul>
                                                <ul class="nav navbar-nav navbar-right"> 
                                                <li id="@Umbraco.Field("dataClass")">
                                                    <a id="@Umbraco.Field("dataId")" 
                                                        data-id="@Umbraco.Field("dataId")" href="#" 
                                                        class="@Umbraco.Field("dataClass") add-to-list btn btn-outline" 
                                                        data-gav="@Umbraco.Field("dataGAID")" 
                                                        data-class="@Umbraco.Field("dataClass")" 
                                                        data-discqty="0" 
                                                        data-qty="@Umbraco.Field("itemQTY")" 
                                                        data-discprice="0" 
                                                        data-price="@Umbraco.Field("itemPrice")" 
                                                        data-desc="@Umbraco.Field("dataDesc")" 
                                                        data-name="@Umbraco.Field("itemName")">Add to list</a></li>
                                                </ul>
                                </div>        
                            </div>
    
            }else if(row.name == "note-image"){ 
                <div id="@row.id" class="sc how-made row left">
                @foreach ( var area in row.areas ) {
                        <div class="col-md-6">
    
                        @foreach (var control in area.controls) {
                            if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            }
                        }
                        </div>
                    }
    
                </div>
            }else if(row.name == "image-note"){ 
                <div id="@row.id" class="sc how-made row right">
                @foreach ( var area in row.areas ) {
    
                        <div class="col-md-6">
    
                        @foreach (var control in area.controls) {
                            if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            }
                        }
                        </div>
                    }
    
                </div>
            }else if(row.name == "top-note-image"){ 
                <div id="@row.id" class="sc how-made unscroll-img unnavigation row center">
                <div class="col-md-12">
                        @foreach ( var area in row.areas ) {
    
                            <div>
                            @foreach (var control in area.controls) {
                                if (control !=null && control.editor != null && control.editor.view != null ) {
                                    <text>@Html.Partial("grid/editors/base", (object)control)</text>
                                }
                            }
                            </div>
                        }
                        </div>
                </div>
            }else if(row.name == "bottom-note-image"){ 
                <div id="@row.id" class="sc how-made unscroll-img unnavigation row center">
                @foreach ( var area in row.areas ) {
    
                        <div class="col-md-12">
    
                        @foreach (var control in area.controls) {
                            if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            }
                        }
                        </div>
                    }
    
                </div>
            }else if(row.name == "next-page"){ 
                <div id="@row.id" class="sc patron unscroll-img unnavigation">
                @foreach ( var area in row.areas ) { 
    
                        <div class="container">
    
                        @foreach (var control in area.controls) {
                            if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            }
                        } 
                        </div>
                    }
    
                </div>
            }else if(row.name == "become-member"){ 
                <div id="@row.id" class="sc patron-small unscroll-img unavigation">
                @foreach ( var area in row.areas ) {
    
                        <div class="container">
    
                        @foreach (var control in area.controls) {
                            if (control !=null && control.editor != null && control.editor.view != null ) {
                                <text>@Html.Partial("grid/editors/base", (object)control)</text>
                            }
                        }
                        </div>
                    }
    
                </div>
            }
    }
    
    @functions 
    {
        public static MvcHtmlString RenderElementAttributes(dynamic contentItem)
        {
            var attrs = new List<string>();
            JObject cfg = contentItem.config;
    
            if(cfg != null) 
            {
                foreach (JProperty property in cfg.Properties()) 
                {
                    attrs.Add(property.Name + "='" + property.Value.ToString() + "'");
                }
            }
    
            JObject style = contentItem.styles;
    
            if (style != null) 
            { 
                var cssVals = new List<string>();
                foreach (JProperty property in style.Properties()) 
                {
                    cssVals.Add(property.Name + ":" + property.Value.ToString() + ";");
                }
    
                if (cssVals.Any()) 
                {
                    attrs.Add("style='" + string.Join(" ", cssVals) + "'");
                }
            }
    
            return new MvcHtmlString(string.Join(" ", attrs));
        }
    }
    

    you can see the code in :

     @foreach (var control in area.controls) {  
                            if(control.value == "itembutton"){
                                <div style="display: none;"><img id='&#105;&#109;&#103;@Umbraco.Field("dataClass")' src="@Umbraco.Media(Umbraco.Field("cartImage")).url" alt="" rel="1948" data-id="1948"></div>
                                <div id="@Umbraco.Field("dataClass")"> 
                                <p> 
    

    i don't know what the error is because i can't get the error reporting from the server and i don't have the access. i just can try and try. to fix this.

    thanks Riski

  • riski 11 posts 101 karma points
    Jan 09, 2018 @ 04:32
    riski
    0

    anyone can help me, is the code are correct?

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jan 09, 2018 @ 14:06
    Alex Skrypnyk
    0

    Hi Riski

    You don't see errors because you are using dynamic objects, it's impossible to debug and see errors with dynamics, it's one of the reasons why we do not recommend to do it.

    What output of this code?

    /Alex

  • riski 11 posts 101 karma points
    Jan 10, 2018 @ 08:59
    riski
    0

    hi alex

    the output from this code just give me the blank page,

    riski

  • Allan 42 posts 192 karma points
    Jan 03, 2018 @ 09:14
    Allan
    0

    My guess here is that you are pulling the image from the media folders using a picker inside the content/page which simply returns the id of that media item...

    To get more data (like the src) from that item you need to do something like...

    var myImg = Umbraco.Media(***id[2311]***);
    <img src="@myImg.url"/>
    

    See https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/media-picker for more info.

  • Sebastian Dammark 581 posts 1385 karma points
    Jan 09, 2018 @ 14:28
    Sebastian Dammark
    0

    Shouldn't it be like this: @Umbraco.Media(Umbraco.Field("cartImage")).Url

    Url with a capital U ?

    https://our.umbraco.org/documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jan 09, 2018 @ 14:41
    Alex Skrypnyk
    0

    It should be with capital U

  • riski 11 posts 101 karma points
    Jan 17, 2018 @ 04:19
    riski
    0

    i use capital U for umbraco and for url with or not with capital U this work

    src="@Umbraco.Media(2311).Url"

    *i insert number and its work

    but if i use (Umbraco.Field("cartImage")) not number this not work

    src="@Umbraco.Media(Umbraco.Field("cartImage")).Url"

  • riski 11 posts 101 karma points
    Jan 17, 2018 @ 10:39
    riski
    100

    solve.

    I've found the solution, I fix it by adding tostring().

    var imgid = Umbraco.Field("cartImage");
    var imgurl = Umbraco.Media(imgid.ToString()).Url;
    

    and call it in img

    <img src="@imgurl" alt="">
    

    thanks

Please Sign in or register to post replies

Write your reply to:

Draft