已向控制器发送post-request@action

本文关键字:post-request@action 控制器 | 更新日期: 2023-09-27 18:03:33

我想向AdminController发送POST请求。但当我在调试器中观察它时,请求是GET。

<form method="post">
<input type="button" formmethod="post" onclick="location.href='@Url.Action("Index","Admin",new {rowID = @p.ProductID})'" value="Delete"/>
</form>

已向控制器发送post-request@action

因为您编写了在提交按钮上执行GET请求的代码,请单击!

onclick="location.href='@Url.Action("Index","Admin",new{rowID=@p.ProductID}('">

在这里,您将location.href值设置为/Admin/Index,这将是一个新的GET请求。

如果要发布,只需删除按钮上的onclick事件即可。如果你想发送ProductID值,你可以将其保存在表单中的一个隐藏输入字段中,当你单击提交时,这个表单元素的值也会被提交。

@using(Html.BeginForm("Index","Admin"))
{
  <input type="hidden" name="rowID" value="@p.ProductID" />
  <input type="submit" value="Delete"/>
}

假设AdminController的HttpPost Index操作方法有一个与输入名称同名的参数来接受productId。

[HttpPost]
public ActionResult Index(int rowID)
{
  // to do : Return something
}