简体   繁体   中英

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? Or do I somehow need to send request from javascript to see updated values? Or could this be done by using C# only?

 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:

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

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. 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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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