Copied to clipboard

Flag this post as spam?

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


  • Greg 18 posts 38 karma points
    Dec 11, 2014 @ 20:25
    Greg
    0

    Splitting up comma separated value from multiple dropdown in Razor

    Total Umbraco noob here and also new to Razor (I'm a frontend and PHP dev).

    So I created a document type for one of my pages... App Category. It's a multiple dropdown list where I select all the applicable categories for these applications. If I do this in my view...

    @Umbraco.Field("appCategory")

    I get...

    Small Business Apps,Accounting Apps,Franchise Apps,Non-profit Apps

     

    What is the proper way to create an array from this comma separated list that I can then use in a foreach? Sorry... again... total noob to .Net and I'm confused.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 11, 2014 @ 20:42
    Dennis Aaen
    0

    Hi Greg and welcome to our,

    You need to use a little bit more code, and there a multiple options for place your code. You could place it directly into your template, our you could create a partial view or partial view macro.

    The documentation for the dropdown list multiple, publish keys can you find here: http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors-v7/DropDown-List-Multiple-Publish-Keys or the dropdown list multiple http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors-v7/DropDown-List-Multiple

    And since your are new to Umbraco I think that you should have a look at Umbraco TV, this is video tutourials about Umbraco, and  there are some free videos about the basic concepts of Umbraco http://umbraco.tv/videos/umbraco-v7/content-editor/ and http://umbraco.tv/videos/umbraco-v7/implementor/. One of the milestones in Umbraco is the concept called Document types, here you define fields for the user in the backoffice. http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/document-types/what-is-a-document-type/

    Some of the videos are requiring a subscription to Umbraco TV, and you can find the prices here. http://umbraco.tv/plans-signup/

    Another good starting point would be http://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/

    You are always welcome to write on the forum, too. We are friendly people. who try to help each other. So if you have other questions just create a topic on the forum.

    Hope this helps,

    /Dennis

  • John A 20 posts 44 karma points
    Dec 11, 2014 @ 21:32
    John A
    0

    Hi Greg,

    Great direction from Dennis for best practices.

    "What is the proper way to create an array from this comma separated list that I can then use in a foreach?" - here's one way..

    @{ 
       string[] categories = Umbraco.Field("appCategory").ToString().Split(',');
       foreach(string category in categories)
       {
         //Do something with category here...
       }
    }
    
  • Greg 18 posts 38 karma points
    Dec 11, 2014 @ 22:02
    Greg
    0

    Thanks guys, I do need to just find my way around the info. I actually bought a year long membership to umbraco.tv.

    My syntax was off using Split() but I was in the ball park. I really appreciate the help... I kind of went into this kicking and screaming but I actually am start to really like working with this. I think my issue is more getting around Razor. I got the site completely scaffolded... now I just dealing with random issues.

  • Charles Afford 1163 posts 1709 karma points
    Dec 12, 2014 @ 13:25
    Charles Afford
    0

    Greg what property type is appCategory?

    If its a Multi Node Tree Picker. You are better creating some complied code that takes in your IPublishedContent, does the split and return you a enumeration of the individual strings.

    Charlie :)

  • Gísli Freyr Svavarsson 43 posts 126 karma points
    Jun 25, 2015 @ 13:54
    Gísli Freyr Svavarsson
    0

    Hey folks

    I'm having a bit of trouble with splitting a string at a comma. I get the correct string from the api (Mon,Wed,Fri,Sun) but when I try to split it and foreach the days the macro crashes and the logs give a CS1513 error.

    Does anyone have a solution for this as I can't seem to find any.

    Here's my code.

    @{
        if (service.Properties != null && service.Properties.Where(i => i.ID == 331).FirstOrDefault() != null) {
            var categories = @service.Properties.Where(i => i.ID == 331).FirstOrDefault().Value.ToString().Split(",");
            foreach (var category in categories){
                <span>@category</span>
            }
        }
    }
    
  • Gísli Freyr Svavarsson 43 posts 126 karma points
    Jun 25, 2015 @ 14:04
    Gísli Freyr Svavarsson
    0

    Btw I've also tried to have a string[] instead of var in categories and in category . So that's not the issue

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jun 25, 2015 @ 14:18
    Dennis Aaen
    0

    Hi Gisli,

    Don´t know if the can be the problem, but could you try the code below. I have removed the @ before in the variable called categories because you are already in the Razor context.

    @{
        if (service.Properties != null && service.Properties.Where(i => i.ID == 331).FirstOrDefault() != null) {
            var categories = service.Properties.Where(i => i.ID == 331).FirstOrDefault().Value.ToString().Split(",");
            foreach (var category in categories){
                <span>@category</span>
            }
        }
    }
    

    Hope this helps,

    /Dennis

  • Gísli Freyr Svavarsson 43 posts 126 karma points
    Jun 25, 2015 @ 15:07
    Gísli Freyr Svavarsson
    0

    thanks for the reply Dennis but my problem was the " in Split(",") apparently I have to use ' instead to get it to work for me..

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft