How to pass multiple checkbox parameters from foreach

Mischa Morf

I have a form in my View with a table and on each cell i have a checkbox. I already have individual Ids for every single checkbox but I dont know how to pass them individually to controller action. I know how to pass single parameters over the "name" attribute but Im not sure on how to handle it with so many diffrent checkboxes.

View

@{
     bool IsOwnRegistration = false;
     foreach (var item in Model.Events.Where(i => i.UserId == Model.UserID && Convert.ToDateTime(i.Date) > dateTime))
     {
       if (item.HasCreatedOtherUsers == null)
       {
         IsOwnRegistration = true;
       }
       string Surname = "";
       string Lastname = "";
       <tr>
           @{
             foreach (var Useritem in Model.Users.Where(i => i.UserId == item.HasCreatedOtherUsers))
             {
               Surname = Useritem.Vorname;
               Lastname = Useritem.Nachname;
             }
             if (IsOwnRegistration == true)
             {
               <th style="background-color:grey; width:33%;">
                 Meine Reservation
               </th>
               <th style="width:33%;">@item.Date</th>
               <th style="width:33%;">
                 <div class="custom-control custom-checkbox ">
                   <input type="checkbox" class="custom-control-input" name="@item.EventId" id="@[email protected]">
                   <label class="custom-control-label" for="@[email protected]"><i style="color:red;" class="fas fa-trash-alt"></i> 
                   </label>
                 </div>
               </th>
             }
             else
             {
               <th style="width:33%;">@Surname @Lastname</th>
               <th style="width:33%;">@item.Date</th>
               <th style="width:33%;">
                 <div class="custom-control custom-checkbox ">
                   <input type="checkbox" class="custom-control-input" name="@item.EventId" id="@[email protected]">
                   <label class="custom-control-label" for="@[email protected]"><i style="color:red;" class="fas fa-trash-alt"></i> 
                   </label>
                 </div>
               </th>
             }
           }

Controller

public ActionResult DeleteRegistrations(Need to get values of all checkboxes) 
{
  return RedirectToAction("HomePage");
}
Markus

In order to transmit the selected checkboxes to the action method, the checkboxes should share a common name that matches the parameter name of the action method. The following sample shows a Razor view that lists some strings in a table, creating a checkbox for each string:

@model  IEnumerable<string>
@{
    ViewBag.Title = "Home Page";
}

<div class="row">
    <div class="col-md-12">
        @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        {
            <table>
                @foreach (var s in Model)
                {
                    <tr>
                        <td>
                            <input type="checkbox" name="selectedValues" value="@s" id="@s" />
                            <label for="@s">@s</label>
                        </td>
                    </tr>
                }
            </table>
            <input type="submit" />
        }
    </div>
</div>

Please note that the name attribute of the checkboxes is set to "selectedValues", the value attribute is set to the original string (or an id in a more complex scenario).

The form is submitted by a POST request to the following action method:

[HttpPost]
public ActionResult Index(IEnumerable<string> selectedValues)
{
    return View(selectedValues);
}

In the POST request, all the values of all checked checkboxes are transmitted as key value pairs in the form "name=value". As all of the checkboxes share the same name, ASP.NET MVC can deserialize this into an IEnumerable<string>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass multiple arguments to foreach from a file

From Dev

How to pass the value from input checkbox to URL Parameters in jquery

From Dev

How to pass multiple parameters from Single Activity to multiple Fragments

From Dev

how i set angulrajs checkbox ng-model to multiple select in laravel foreach and pass values to angularjs controller

From Dev

mysqli insert into database from multiple checkbox with foreach

From Dev

How to pass multiple parameters from thymeleaf html to thymeleaf dialect processor

From Dev

How to pass multiple parameters from robot script to python

From Dev

How to pass multiple parameters from angular 8 to .NET Core API

From Dev

How to pass multiple parameters from .Net console app to Class Library?

From Dev

How to pass multiple parameters to a function from HTML during Onclick

From Dev

How to pass multiple parameters from Angular form to make POST request?

From Dev

How to pass multiple parameters in controller?

From Dev

How to pass multiple parameters in Nuxt?

From Dev

How to pass ForEach parameters to a SwiftUI views?

From Dev

How to pass multiple string values to checkbox?

From Dev

How to pass multiple checkbox values to php

From Dev

How to pass multiple checkbox values into an array

From Dev

How to pass value of multiple checkbox to a model?

From Dev

How to pass parameters from textInput

From Dev

pass multiple parameters from main to a class

From Dev

Laravel Livewire pass multiple parameters from view

From Dev

bash: pass multiple parameters from a single variable

From Dev

Pass multiple parameters from a jQuery function to PHP

From Dev

how to pass checkbox array to php from FormData

From Dev

How to pass multiple list values as function parameters?

From Dev

How to pass multiple GET parameters to Express?

From Dev

How to pass multiple parameters with JSON on Servlet to Ajax

From Dev

How to pass multiple parameters to a Cucumber step?

From Dev

how to pass multiple url parameters in django

Related Related

  1. 1

    How to pass multiple arguments to foreach from a file

  2. 2

    How to pass the value from input checkbox to URL Parameters in jquery

  3. 3

    How to pass multiple parameters from Single Activity to multiple Fragments

  4. 4

    how i set angulrajs checkbox ng-model to multiple select in laravel foreach and pass values to angularjs controller

  5. 5

    mysqli insert into database from multiple checkbox with foreach

  6. 6

    How to pass multiple parameters from thymeleaf html to thymeleaf dialect processor

  7. 7

    How to pass multiple parameters from robot script to python

  8. 8

    How to pass multiple parameters from angular 8 to .NET Core API

  9. 9

    How to pass multiple parameters from .Net console app to Class Library?

  10. 10

    How to pass multiple parameters to a function from HTML during Onclick

  11. 11

    How to pass multiple parameters from Angular form to make POST request?

  12. 12

    How to pass multiple parameters in controller?

  13. 13

    How to pass multiple parameters in Nuxt?

  14. 14

    How to pass ForEach parameters to a SwiftUI views?

  15. 15

    How to pass multiple string values to checkbox?

  16. 16

    How to pass multiple checkbox values to php

  17. 17

    How to pass multiple checkbox values into an array

  18. 18

    How to pass value of multiple checkbox to a model?

  19. 19

    How to pass parameters from textInput

  20. 20

    pass multiple parameters from main to a class

  21. 21

    Laravel Livewire pass multiple parameters from view

  22. 22

    bash: pass multiple parameters from a single variable

  23. 23

    Pass multiple parameters from a jQuery function to PHP

  24. 24

    how to pass checkbox array to php from FormData

  25. 25

    How to pass multiple list values as function parameters?

  26. 26

    How to pass multiple GET parameters to Express?

  27. 27

    How to pass multiple parameters with JSON on Servlet to Ajax

  28. 28

    How to pass multiple parameters to a Cucumber step?

  29. 29

    how to pass multiple url parameters in django

HotTag

Archive