Copied to clipboard

Flag this post as spam?

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


  • Josip 195 posts 662 karma points c-trib
    May 13, 2019 @ 17:03
    Josip
    0

    Display nodes as checkbox list

    Hello guys i am stuck with this realy hard.

    I will try to explain my problem the best i know (with my poor english).

    I have nodes (categories) and i have members with MNTP property which is linked to this nodes (categories).

    I mananged to display all categories and found which category is picked by member in my view but i dont know how to display checkbox as checked if its picked by member and than after submiting the form pass this data to controller so i can save this value to MNTP.

    This is what i have don for now:

    View:

        @using (Html.BeginUmbracoForm<CategoriesController>("SaveCategories"))
    {
    
            // GET ALL CATEGORIES FROM CATEGORY LIST
            var categories = Umbraco.Content(Guid.Parse("7ad9518b-2069-4ac5-9035-e052decf6e05")).Children().Where(x => x.IsVisible() && x.Name != null).ToArray();
    
            // GET MEMBER BY ID
            var member = Services.MemberService.GetById(1148);
    
            // CREATE LIST OF MEMBER CATEGORIES
            List<IPublishedContent> memberCategories = new List<IPublishedContent>();
    
            // GET MEMBER PROPERTY WITH LIST OF UDI-s
            var memberCategoriesUdi = member.GetValue<string>("categoryPicker");
    
            // CHECK IF MEMBERPROPERTY IS EMPTY
            if (memberCategoriesUdi != null)
            {
                foreach (var udi in memberCategoriesUdi.Split(','))
                {
                    //OCNVERT UDI TU IPublishedContent
                    var cer = Umbraco.Content(Udi.Parse(udi)).DescendantsOrSelf()
                                                     .Where(x => x.IsVisible()).ToArray(); ;
    
                    foreach (var category in cer)
                    {
                        //ADD CATEGORIES TO LIST
                        memberCategories.Add(category);
                    }
                }
            }
    
    
        @* Ensure that the Current Page has children *@
        if (categories.Length > 0)
        {
            @* Get the first page in the children, where the property umbracoNaviHide is not True *@
            var naviLevel = categories[0].Level;
    
            @* Add in level for a CSS hook *@
            <ul class="list-group list-group-flush level-@(naviLevel)">
                @* Loop through the selection *@
                @foreach (var item in categories)
                {
                    //IF MEMBER HAS ALREDY PICKED THAT CATEGORY SHOW THIS:
                    <li>
                        @if (memberCategories.Contains(item))
                        {
                            @Html.CheckBoxFor(m => m.IsSelected, new { @checked = true }) @item.Name
                        }
                        else
                        {
                            @Html.CheckBoxFor(m => m.IsSelected, new { @checked = false }) @item.Name
                        }
    
                        @* if this child page has any children, where the property umbracoNaviHide is not True *@
                        @{
                            var children = item.Children.Where(x => x.IsVisible() && x.Name != null).ToArray();
                            if (children.Length > 0)
                            {
                                @* Call our helper to display the children *@
                                @ChildPages(children)
                            }
                        }
                    </li>
                }
            </ul>
        }
    
        <button type="submit" class="btn btn-success btn-block"><i class="px-2 fa fa-sign-in fa-lg"></i>save</button>
    }
    

    Model:

      public class MemberCategories
    {
        public int CategoryId { get; set; }
        public bool IsSelected { get; set; }
    }
    public class categories
    {
        public List<MemberCategories> SelectedCategories { get; set; }
    }
    

    Controller:

     [HttpPost]
        public ActionResult SaveCategories(categories model)
        {
            if (ModelState.IsValid)
            {
    
            }
                return CurrentUmbracoPage();
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft