MVC C# 中的多个复选框 select

Multiple checkbox select in MVC C#

我有 table 如下所示。我想 check/uncheck(现在只是尝试检查)table 中基于类型 I select 的值。这可以像这样以某种方式完成吗?或者我是否需要以某种方式从 javascript 发送请求以查看更新的值?还是只能使用 C# 来完成?

var list = JsonConvert.SerializeObject(@table);
            <form asp-controller="Consumer" asp-action="" method="post">
                <div class="row justify-content-end" style="margin-top: 20px; margin-bottom: 20px;">
                    @foreach (var type in ObjectTypes.All)
                    {
                        <label style="margin-right: 10px;">
                            <input onclick="selectType(@list, @type)" style="margin-right: 5px;" type="checkbox" id="@type" name="type" value="@type">@type
                        </label>
                    }
                </div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th></th>
                        <th>Object Id</th>
                        <th>Object type</th>
                    </tr>
                    </thead>
                    <tbody>
                    @if (table.Count > 0)
                    {
                        @for (int i = 0; i < table.Count; i++)
                        {
                            <input type="hidden" name="@("items[" + i + "].ObjectId")" value="@table[i].ObjectId"/>
                            <input type="hidden" name="@("items[" + i + "].ObjectType")" value="@table[i].ObjectType"/>
                            <tr>
                                <td>@(++id)</td>
                                <td>
                                    <input name="@("items[" + i + "].Checked")" type="checkbox" value="true" @(table[i].Checked ? "checked" : " ")/>
                                </td>
                                <td>@table[i].ObjectId</td>
                                <td>@table[i].ObjectType</td>
                            </tr>
                        }
                    }
                    </tbody>
                </table>
            </form>

我有这样的js函数:

function selectType(items, type)
{
   return items.filter(x => x.ObjectType === type).map(x => x.Checked = true);
}

但我需要将 checked 属性添加到复选框输入,但我不知道该怎么做

根据 this documentation,我可以看到您可以使用以下方式切换“选中的属性”:

document.getElementById("myCheck").checked = true;

但我不明白为什么要在 selectType 方法中 return 某些东西。这是当您单击复选框时调用的方法,因此您不需要 return 任何东西。您只需要更改此标记的 Checked 属性。类似的东西应该可以解决问题:

function selectType(items, type)
{
   document.getElementById(type).checked = true; // I assume type is the Id of the element
}