2016-08-23 87 views
0

我想发送POST请求到AdminController。但是当我在调试器中看到它时,请求是GET。发送了请求@action到控制器

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

它是确定动词的控制器,而不是html表单。用'[HttpPost]'装饰你的控制器动作 – Crowcoder

回答

0

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

的onclick = “location.href = '@ Url.Action( ”指数“, ”管理“,新{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操作方法有相同的名字输入名称,接受产品ID的参数。

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