如何将节点显示为复选框列表

时间:2019-05-14 11:05:28

标签: c# asp.net-mvc razor umbraco

我读过一些类似的文章,但无法为我工作。

我将尽我所能解释我的问题。

我有节点(类别),并且成员具有MNTP属性,该属性链接到该节点(类别)。

我想显示所有类别,并发现成员在我的视图中选择了哪个类别,但是我不知道如何显示复选框(如果它被成员选中),并且在提交表格后将此数据传递给控制器​​,这样我就可以保存该值发送给MNTP。

这是我现在不知道的东西:

查看:

    @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);
                    }
                }
            }


        if (categories.Length > 0)
        {

            var naviLevel = categories[0].Level;


            <ul class="list-group list-group-flush level-@(naviLevel)">
                @* Loop through the selection *@
                @foreach (var item in categories)
                {

                    <li>
                        @if (memberCategories.Contains(item))
                        {
//IF MEMBER HAS ALREDY PICKED THAT CATEGORY SHOW THIS:
                            @Html.CheckBoxFor(m => m.IsSelected, new { @checked = true }) @item.Name
                        }
                        else
                        {
                            @Html.CheckBoxFor(m => m.IsSelected, new { @checked = false }) @item.Name
                        }

                        @{
                            var children = item.Children.Where(x => x.IsVisible() && x.Name != null).ToArray();
                            if (children.Length > 0)
                            {
                                @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>
    }

型号:

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

控制器:

 [HttpPost]
    public ActionResult SaveCategories(categories model)
    {
        if (ModelState.IsValid)
        {

        }
            return CurrentUmbracoPage();
    }

1 个答案:

答案 0 :(得分:0)

尝试以下代码更改:

@if (memberCategories.Contains(item))
{
    //IF MEMBER HAS ALREDY PICKED THAT CATEGORY SHOW THIS:
    @Html.CheckBoxFor(m => m.IsSelected, new { @checked = true }) @item.Name
}
else
{
    @Html.CheckBoxFor(m => m.IsSelected, new { @checked = false }) @item.Name
}

应该是:

@if (memberCategories.Any(i => i.Id == item.Id))
{
    //IF MEMBER HAS ALREDY PICKED THAT CATEGORY SHOW THIS:
    @Html.CheckBoxFor(m => m.IsSelected, new { @checked = true }) @item.Name
}
else
{
    @Html.CheckBoxFor(m => m.IsSelected, new { @checked = false }) @item.Name
}

一个.contains必须是一个完全匹配项,但有时不同集合中的相同项之间没有完全匹配项。