Copied to clipboard

Flag this post as spam?

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


  • mizzle 90 posts 325 karma points
    Feb 21, 2020 @ 21:42
    mizzle
    0

    List all Checkbox List options/Compare two lists within one foreach.

    I'm using a checkbox list to select "active months" and then highlight those active months within a section that lists all the months. I can't find a way to list all the months in the checklist within the same foreach loop, but change the styles for the selected months.

    So, I'm listing months January through December and if the user selects April, May, and June, I want the styles for those months to change - bold instead of regular font, for example. So I'd want it to look like this:

    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)

    I can get the selected months with:

    List<String> monthList = Model.Value<IEnumerable<string>>("monthSelect").ToList();
    

    And I can hard-code all the months into a list that can then be iterated through with a foreach, but I don't know how to apply the selected month information to the full list of months in a way that keeps everything within the same section and only changes styling.

  • Neil Hodges 338 posts 987 karma points
    Feb 23, 2020 @ 11:23
    Neil Hodges
    0

    HI mizzle

    Not 100% sure what you mean. However, is it that in your foreach loop you need to change the styling of selected months?

    If so, when iterating over your foreach loop can you set a class on the checkbox as active, and CSS you can then make that set to be bold.

    Sorry if I'm not understanding correctly.

    Could you share the foreach loop you have so far?

  • mizzle 90 posts 325 karma points
    Feb 24, 2020 @ 14:18
    mizzle
    0

    Yes, in my foreach loop I need to detect which months are selected on the page and then change the styling of those months.

    Currently I can loop through the months, which are written directly into a list in my code since I can't find a way to detect the non-selected months from the checkbox list. That's how I get the full list of months from January to December.

    I can also foreach through the selected checkbox values with

    List<String> monthList = Model.Value<IEnumerable<string>>("monthSelect").ToList();
    
    foreach (var month in monthList) 
    {
          @*do something with month*@
    }
    

    But I don't know what code I need to use to essentially say "If a month in the Full List is the same in Selected List, change the class, while still listing out every month, in order, as if foreach looping through the Full List."

Please Sign in or register to post replies

Write your reply to:

Draft