简体   繁体   English

MVC C# 中的多个复选框 select

[英]Multiple checkbox select in MVC C#

I have table like showed below.我有如下所示的表格。 I want to check/uncheck (for now just trying out check) values in the table based on the type I select. Could this be done somehow like this?我想根据 I select 类型检查/取消检查(现在只是尝试检查)表中的值。可以这样完成吗? Or do I somehow need to send request from javascript to see updated values?或者我是否需要以某种方式从 javascript 发送请求以查看更新的值? Or could this be done by using C# only?或者这可以通过仅使用 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>

I had such js function:我有这样的 js function:

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

But I need to add checked attribute to the checkbox input and I have no idea how that should be done但是我需要在复选框输入中添加checked属性,但我不知道该怎么做

According to this documentation , I can see you are able to switch the "checked attribute" using:根据此文档,我可以看到您可以使用以下方式切换“选中的属性”:

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

But I do not get the reason why you want to return something in your selectType method.但我不明白为什么你想在你的selectType方法中返回一些东西。 It is the method called when you will click on a checkbox, so you do not need to return anything.这是当您单击复选框时调用的方法,因此您不需要返回任何内容。 You will just need to change the Checked attribute for this tag.您只需要更改此标记的 Checked 属性。 Something like that should do the trick:像这样的东西应该可以解决问题:

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

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

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