single form and multiple submit (with multiple value)

ghz 7months ago ⋅ 138 views

I Have a "For Loop" In ASP.NET Core Like This :

<button type="submit" Form="FormActive" name="id" value="@item.I"></button>
 <!--It Will Create SomeThing Like This:-->
 <button type="submit" Form="FormActive" name="id" value="1"></button>
 <button type="submit" Form="FormActive" name="id" value="2"></button>
 <button type="submit" Form="FormActive" name="id" value="3"></button>
  ...          ...               ...        ...           4
  ...          ...               ...        ...           99
<!--And This Is My Single Form : -->
    <form id="FormActive" action="/action_page">
</form>

Whats The Problem? why This Form Just Send A httpRequest POST Without"ID" And Value?(infact It Will Send A Post Request With Empty Body)

What I Should To do for send ID

Edit: I cant Remove Form - And I Cant Use +99 Form too maybe i need jquery ... or something?

Answers

It seems like you're trying to submit a form with multiple buttons, each with a different value for the id parameter. However, when you submit the form, the id parameter is not being sent in the request.

The reason for this behavior is that only the button that was clicked is included in the form submission, along with its name and value. In your case, since you have multiple buttons with the same name (id), only the value of the button that was clicked is sent in the request.

To solve this issue, you can use JavaScript or jQuery to dynamically set the id parameter value before submitting the form. Here's how you can do it with jQuery:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  $(document).ready(function() {
    $('button[type="submit"]').click(function() {
      $('#FormActive').append('<input type="hidden" name="id" value="' + $(this).val() + '">');
      $('#FormActive').submit();
    });
  });
</script>

This script listens for click events on all submit buttons within your form. When a button is clicked, it appends a hidden input field with the name id and the value of the clicked button to the form. Then, it submits the form with the updated input field.

With this approach, you don't need to change the structure of your form or add multiple forms. It dynamically adds the id parameter to the form before submission, ensuring that it is included in the request.