如何在 mvc 4 中的表单提交上调用 POST 控制器而不是 GET 方法
How to call POST controller instead of GET method on form submit in mvc 4
我在提交表单时调用 POSt 控制器方法时遇到问题。下面是我的两种方法
[HttpGet]
public ViewResult List(int page = 1)
{
}
[HttpPost]
public ViewResult List(SearchTerms search,int page = 1)
{
}
在 http 请求中,我的 GET 方法被调用了。但是,当我提交表单时,我希望调用 POST 方法,但再次调用相同的 GET 方法。 post 方法永远不会被调用。请问我哪里出错了?任何帮助表示赞赏。下面是我的表格。
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
......
<span class="input-group-btn">
<button class="btn btn-info" type="button" id="addressSearch" onclick="location.href='@Url.Action("List", "Search")'">Search</button>
</span>
...
}
您的按钮没有提交表单。它重定向到 List 页面,这使浏览器发送 GET 请求。您可以使按钮提交表单:
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
......
<span class="input-group-btn">
<button class="btn btn-info" type="submit" id="addressSearch">Search</button>
</span>
...
}
我在提交表单时调用 POSt 控制器方法时遇到问题。下面是我的两种方法
[HttpGet]
public ViewResult List(int page = 1)
{
}
[HttpPost]
public ViewResult List(SearchTerms search,int page = 1)
{
}
在 http 请求中,我的 GET 方法被调用了。但是,当我提交表单时,我希望调用 POST 方法,但再次调用相同的 GET 方法。 post 方法永远不会被调用。请问我哪里出错了?任何帮助表示赞赏。下面是我的表格。
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
......
<span class="input-group-btn">
<button class="btn btn-info" type="button" id="addressSearch" onclick="location.href='@Url.Action("List", "Search")'">Search</button>
</span>
...
}
您的按钮没有提交表单。它重定向到 List 页面,这使浏览器发送 GET 请求。您可以使按钮提交表单:
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
......
<span class="input-group-btn">
<button class="btn btn-info" type="submit" id="addressSearch">Search</button>
</span>
...
}