Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

What I have tried:

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


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>
Posted
Updated 8-May-22 23:35pm

1 solution

Maybe the form collection is a solution:

var selectedKeys = Request.Form.Keys.Where(k=> k.EndsWith(".Checked", System.StringComparison.OrdinalIgnoreCase));


Returns:
1.Checked
3.Checked
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900