Get values using multiplus buttons inside a foreach core

I have multiplus buttons in a foreach and, when I click at one, need to submit/send his data- values to an action/method. How can I do this?

  <form asp-area="App" id="formMenu">      @foreach (var item in ViewBag.Atendimento)      {              @* I was doing with inputs, but with more than one button, I can't do it anymore              <input value="@item.IdProduto" name="id" type="hidden" />              <input value="@item.IdProdutoMeia" name="idMeio" id="idMeio" type="hidden" />              <input value="@item.Quantidade" name="Qtd" type="hidden" />               <button class="btn btn-info btn-sm" id="btnEdit">                  Edit              </button>              *@               @* So, I started trying like this *@              <button class="btn btn-outline-info btn-sm" onClick="btnEdit(this)"  data-id="@item.IdProduto" data-idmeio="@item.IdProdutoMeia" data-qtd="@item.Quantidade">                 <i class="fas fa-pen fa-sm"></i>              </button>      }  </form>    <script type="text/javascript">         // With just one button, like I sad, it was working fine like this        $("#btnEdit").click(function () {             if ($("#idMeio").val() == '') {                 $("#formMenu").attr("action", "@Url.Action("Edit", "Produto")");             } else {                 $("#formMenu").attr("action", "@Url.Action("EditMeio", "Produto")");             };             $("#formMenu").submit();         });        // So I have been traying this way, but don't know how to do      function btnEdit(event) {             var button = $(event.relatedTarget)             var idItem = button.attr('data-id')             var idItemMeio = button.attr('data-idmeio')             var qtdItem = button.attr('data-qtd')             ???     };     </script> 

Please help with some idea, code etc

Add Comment
1 Answer(s)

If there are multiple buttons and all the buttons are with tag <button> then apply click function with button tag and get button id. On the basis of button id, related URL can be set to call Action method.

$("button").click(function () {             alert(;             var btnId =         }); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.