请问有人能告诉我如何使用ActionLink和POST方法将值提交给控制器吗?
我不想使用按钮。
我猜这与jquery有关。
请问有人能告诉我如何使用ActionLink和POST方法将值提交给控制器吗?
我不想使用按钮。
我猜这与jquery有关。
jquery.unobtrusive-ajax.min.js
"。否则,当单击时它将继续执行GET而不是POST。创建链接的语法如下:@Ajax.ActionLink("Delete", "Delete", new { id = item.Id }, new AjaxOptions {HttpMethod = "POST"})
。 - CodingWithSpike你不能使用ActionLink
,因为那只会渲染一个锚点<a>
标签。
你可以使用jQuery AJAX post。
或者直接调用表单的提交方法,可以使用jQuery或者不使用jQuery(这将不是AJAX),也许在任何你喜欢的控件的onclick
事件中调用。
你可以使用jQuery为所有按钮执行POST请求。只需给它们相同的CssClass名称即可。
如果你想在提交后执行服务器端的RedirectToAction,只需在onclick javascript事件的末尾使用"return false;",否则只需返回视图。
Razor代码
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.ID)
@Html.ActionLink("Save", "SaveAction", "MainController", null, new { @class = "saveButton", onclick = "return false;" })
}
JQuery代码
$(document).ready(function () {
$('.saveButton').click(function () {
$(this).closest('form')[0].submit();
});
});
C#
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveAction(SaveViewModel model)
{
// Save code here...
return RedirectToAction("Index");
//return View(model);
}
@Aidos给出了正确答案,只是想澄清一下,因为它被@CodingWithSpike放在他的帖子的评论中隐藏起来了。
@Aidos有正确答案,但由于它被@CodingWithSpike放在他的帖子评论中,所以我想让它更加清晰明了。
@Ajax.ActionLink("Delete", "Delete", new { id = item.ApkModelId }, new AjaxOptions { HttpMethod = "POST" })
我认为在默认的ASP.NET MVC 5项目中,有一个答案可以很好地实现我的UI样式目标。使用纯JavaScript提交表单到某个包含表单的位置。
@using (Html.BeginForm("Logout", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
<a href="javascript:document.getElementById('logoutForm').submit()">
<span>Sign out</span>
</a>
}
@using (Html.BeginForm("Logout", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<div class="dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="ma-nav-text ma-account-name">@User.Identity.Name</span>
<i class="material-icons md-36 text-inverse">person</i>
</button>
<ul class="dropdown-menu dropdown-menu-right ma-dropdown-tray">
<li>
<a href="javascript:document.getElementById('logoutForm').submit()">
<i class="material-icons">system_update_alt</i>
<span>Sign out</span>
</a>
</li>
</ul>
</div>
}
ActionLink永远不会触发POST请求,它总是触发GET请求。
@if (ViewBag.ShowRemoveButton)
{
using (Html.BeginForm("RemoveLogin", "Manage"))
{
@Html.AntiForgeryToken()
<div>
@Html.Hidden("company_name", account)
@Html.Hidden("returnUrl", Model.returnUrl)
<input type="submit" class="btn btn-default" value="Remove" title="Remove your email address from @account" />
</div>
}
}
使用以下内容调用操作链接:
<%= Html.ActionLink("Click Here" , "ActionName","ContorllerName" )%>
要提交表单值,请使用:
<% using (Html.BeginForm("CustomerSearchResults", "Customer"))
{ %>
<input type="text" id="Name" />
<input type="submit" class="dASButton" value="Submit" />
<% } %>
在 Ajax.BeginForm 中使用此链接
@Html.ActionLink(
"Save",
"SaveAction",
null,
null,
onclick = "$(this).parents('form').attr('action', $(this).attr('href'));$(this).parents('form').submit();return false;" })
;)
@using (Html.BeginForm("Delete", "Admin"))
{
@Html.Hidden("ProductID", item.ProductID)
<input type="submit" value="Delete" />
}