skip to Main Content

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?

2

Answers


  1. You have to use input tag with type="submit" name="id" value="1"
    Buttons does not have name and value attributes.

    Login or Signup to reply.
  2. How about try asp-action , like

    <form id="FormActive" asp-action="Index">
    </form>
    

    Then in controller:

    [HttpPost]
     public IActionResult Index(int id)
     {
        
         return View();
     }
    

    result:

    enter image description here

    Update:

    Try to use <a> tag without With Tag Form

    <a asp-controller="Home" asp-action="Index2" asp-route-id="1"><button>1</button></a>
    

    Then in controller;

     [HttpGet]
     public IActionResult Index2(int id)
      {
       return View();
       }
    

    result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search