Copied to clipboard

Flag this post as spam?

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


  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 18:13
    Rayyan
    0

    Pick up directory folder dynamically

    Hello, I need to replace the hardcoded folder name with that set by specific docType Property Value, here's my partial view page code,

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/1039");
    }
    @foreach (string item in files){
         <img src="/media/1039/@Path.GetFileName(item)" />
    }  
    

    And I have tried the following but I think that it's missing something,

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/@Model.Content.GetPropertyValue("placeID")");
    }
    @foreach (ver place string item in files){
        <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item)" />
    } 
    

    Thank you,

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Jul 19, 2017 @ 18:20
    Nicholas Westby
    100

    You are not doing what you are expecting with this string:

    "/@Model.Content.GetPropertyValue("placeID")"
    

    Since you are inside of a string in a C# code block, that @Model has no special meaning (i.e., it literally gets interpreted that way, rather than getting evaluated as an expression). You want something like this (I haven't tested this in Visual Studio, so it may have typos):

    string[] files = Directory.GetFiles(folderPath + "/" + Model.Content.GetPropertyValue("placeID"));
    

    Also, you might consider calling GetPropertyValue<string>("placeID") (assuming that field is a string).

    If you store that generated folder path into a variable, you can use the debugger in Visual Studio to set a breakpoint and inspect the value to ensure it is correct.

  • Rayyan 46 posts 238 karma points
    Jul 19, 2017 @ 18:37
    Rayyan
    0

    Awesome! Thank you very much Nicholas!

    And here's the final code, works without <string>

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/" + Model.Content.GetPropertyValue("placeID"));
    }
    @foreach (string item in files){
        <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item)" />
    } 
    
Please Sign in or register to post replies

Write your reply to:

Draft