我有一个"for Loop"在ASP.NET Core中这样:

<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>

什么问题? 为什么这个表格只发送一个httpRequest POST Without"ID"和值?(实际上它将发送一个帖子请求Empty Body)

send ID我该做什么

上一篇:I Can Remove Form—And I Cant Use +99 Form Too 也许我需要jQuery…还是怎么的

推荐答案

试试asp-action个怎么样

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

然后在控制器中:

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

结果:

enter image description here

Update:

try 使用<a>标签不带标签表单

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

然后在控制器;

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

结果:

enter image description here

Update2 我们可以在表单中添加一个隐藏的<input>来获得ID,如下所示:

<button type="submit" Form="FormActive" name="id" value="1">a</button>
<form id="FormActive"  method="post" asp-controller="Home" asp-action="Index" data-ajax="true" data-ajax-method="post" data-ajax-complete="completed">    
    <input id="buttonid"  type="hidden" name="id" />        
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
    $("button").click(function (e) {      
        var fired_button = $(this).val();
        alert(fired_button);
        $('#buttonid').val(fired_button);
    });
</script>

结果:

enter image description here

Html相关问答推荐

需要帮助实现悬停动画效果

如何让QML`Text`元素内的链接在悬停时显示鼠标指针?

附加点的列表样式

为什么在移动视图中,这个水平可滚动的表格会在表格的右侧显示额外的空间?

在窄屏幕上显示表格,每个单元格占一行

滚动平滑在笔记本电脑上不起作用,但在Nextjs网站的手机浏览器中起作用

在Hero部分中同时zoom 背景图像和形状的问题

如何在小屏幕中制作水平滚动div

仅 Select 悬停的级别,而不 Select 其父级或子级

Flex:第一个 div 有列,下一个 div 有行

如何防止弹性项目溢出容器?

在显示 swift ui 之前加载下一个 YouTube 视频

复制两个

会产生一个空行;复制

元素不会

使单行路径的 svg 填充父级的 100% 宽度

子元素放在文本样式之后,样式会 destruct

BeautifulSoup用名称列表的findall无法找到另一个目标后面的目标

如何将 HTML DateTime 转换为 Golang Time 对象

删除按钮组件时 bootstrap col-auto 布局高度对齐中断

辅助功能:alt 或 alt="" 用于装饰图像

CSS Padding 将右对齐的对象推离页面