简体   繁体   English

如何在动态列表组中将某些复选框标记为已选中? .NETCore / Bootstrap / Razor 页面

[英]How do I mark certain checkboxes as checked in a dynamic list group? .NETCore / Bootstrap / Razor Pages

I am building a tool to add contacts to distribution lists and one contact can be in multiple distribution lists.我正在构建一个工具来将联系人添加到通讯组列表中,一个联系人可以在多个通讯组列表中。 I am using a List Group with checkboxes for users to display and select the distribution lists.我正在使用带有复选框的列表组供用户显示,并使用 select 分发列表。 On the edit contact page I need to check the checkboxes in the list group of the distribution lists that the contact is already a member of.在编辑联系人页面上,我需要选中联系人已经是其成员的分发列表的列表组中的复选框。 I simply cannot work out how to do this.我根本无法弄清楚如何做到这一点。 I am using asp.net core with page models (no controllers) and razor pages我正在使用带有页面模型(无控制器)和 razor 页面的 asp.net 内核

This on my cshtml page:这在我的 cshtml 页面上:

<label for="DistributionListSelectIDLG" id="SeletDistributionListLabel" class="control-label">Select a Distribution List</label>
<div class="overflow-auto" style="max-height:500px">
    <ul class="list-group" id="DistributionListSelectIDLG" role="group" aria-labelledby="SeletDistributionListLabel">
        @foreach (var item in Model.CreateContactViewModel.DistributionLists)
        {
            <li class="list-group-item"><input type="checkbox" name="DistributionListSelectID" id="@item.DistributionListId" value="@item.DistributionListId" aria-label="@item.ListName" /> @item.ListName</li>
        }
    </ul>
</div>

This is on my code behind page这是我的代码背后的页面

Contact = await _context.Contacts.FirstOrDefaultAsync(m => m.ContactId == id);

if (Contact == null)
{
    return NotFound();
}

CreateContactViewModel = new ContactViewModel()
            {
                DistributionLists = _distService.GetAllLists().Where(d => d.IsDeleted == false).OrderBy(d => d.ListName).ToList(),
                ListsAndContacts = _linkService.GetListsForContact((int)id).ToList()
            };

foreach (var dlitem in CreateContactViewModel.ListsAndContacts)
{
    // Do something here to populate the checkboxes
}

return Page();

There are two interfaces, one to get the full list of DLs, and one to get a list of DLs that contact belongs to.有两个接口,一个获取DL的完整列表,一个获取联系人所属的DL列表。

Thanks谢谢

On the edit contact page I need to check the checkboxes in the list group of the distribution lists that the contact is already a member of.在编辑联系人页面上,我需要选中联系人已经是其成员的分发列表的列表组中的复选框。

Do you mean check if the name in one distribution list and in a List Group too?您的意思是检查一个分发列表和列表组中的名称吗?

Try to make an if judgment for the checkbox.尝试对复选框进行if判断。 If the name(contact) is already a member of the distribution list, input checkbox will be checked="checked".如果姓名(联系人)已经是分发列表的成员,则输入复选框将被选中=“已选中”。

Demo: The ContactViewModels has 8 items:AA,BB,CC,DD,EE,FF,GG,HH; Demo:ContactViewModels有8个项目:AA,BB,CC,DD,EE,FF,GG,HH; The Contacts has 5 items:AA,BB,CC.DD,EE.联系人有 5 项:AA、BB、CC.DD、EE。

Code:代码:

<label for="DistributionListSelectIDLG" id="SeletDistributionListLabel" class="control-label">Select a Distribution List</label>
<div class="overflow-auto" style="max-height:500px">
    <ul class="list-group" id="DistributionListSelectIDLG" role="group" aria-labelledby="SeletDistributionListLabel">
        @foreach (var item in Model.ContactViewModels)
        {
            <li class="list-group-item">
               @*@if (Model.Contacts.Where(c => c.Name == item.Name).Count() != 0)*@//First method
                @if (Model.Contacts.Any(c => c.Name == item.Name))      //Second method
                {
                  <input type="checkbox" checked="checked" name="DistributionListSelectID" id="@item.Id" value="@item.Id" aria-label="@item.Name" />
                }
                else
                {
                    <input type="checkbox" />
                }

                @item.Name

            </li>
        }
        </ul>
</div>

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM