asp.net-mvc ASP.NET MVC ActionLink 和 post 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2048778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC ActionLink and post method
提问by ?ljaker
Can anyone tell me how can I submit values to Controller using ActionLink and POST method?
I don't want to use buttons.
I guess it has something with jquery.
谁能告诉我如何使用 ActionLink 和 POST 方法向控制器提交值?
我不想使用按钮。
我猜它与jquery有关。
采纳答案by AUSteve
You can't use an ActionLinkbecause that just renders an anchor <a>tag.
You can use a jQuery AJAX post.
Or just call the form's submit method with or without jQuery (which would be non-AJAX), perhaps in the onclickevent of whatever control takes your fancy.
您不能使用 anActionLink因为它只会呈现一个锚<a>标记。
您可以使用jQuery AJAX 帖子。
或者只是使用或不使用 jQuery(这将是非 AJAX)调用表单的提交方法,也许在onclick您喜欢任何控件的情况下。
回答by Aidos
If you're using ASP MVC3 you could use an Ajax.ActionLink(), that allows you to specify a HTTP Method which you could set to "POST".
如果您使用的是 ASP MVC3,您可以使用 Ajax.ActionLink(),它允许您指定一个可以设置为“POST”的 HTTP 方法。
回答by Idris
You can use jQuery to do a POST for all your buttons. Just give them the same CssClass name.
您可以使用 jQuery 为所有按钮执行 POST。只需给它们相同的 CssClass 名称。
Use "return false;" at the end of your onclick javascript event if you want to do a server side RedirectToAction after the post otherwise just return the view.
使用“返回假;” 在您的 onclick javascript 事件结束时,如果您想在发布后执行服务器端 RedirectToAction,否则只需返回视图。
Razor Code
剃刀代码
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.ID)
@Html.ActionLink("Save", "SaveAction", "MainController", null, new { @class = "saveButton", onclick = "return false;" })
}
JQuery Code
查询代码
$(document).ready(function () {
$('.saveButton').click(function () {
$(this).closest('form')[0].submit();
});
});
C#
C#
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveAction(SaveViewModel model)
{
// Save code here...
return RedirectToAction("Index");
//return View(model);
}
回答by goodies4uall
@Aidos had the right answer just wanted to make it clear since it is hidden inside a comment on his post made by @CodingWithSpike.
@Aidos 有正确的答案只是想说清楚,因为它隐藏在@CodingWithSpike 对他的帖子的评论中。
@Ajax.ActionLink("Delete", "Delete", new { id = item.ApkModelId }, new AjaxOptions { HttpMethod = "POST" })
回答by daniel.caspers
Here was an answer baked into the default ASP.NET MVC 5 project I believe that accomplishes my styling goals nicely in the UI. Form submit using pure javascript to some containing form.
这是默认 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>
}
The fully shown use case is a logout dropdown in the navigation bar of a web app.
完整显示的用例是 Web 应用程序导航栏中的注销下拉列表。
@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>
}
回答by Navish Rampal
ActionLink will never fire post. It always trigger GET request.
ActionLink 永远不会触发帖子。它总是触发 GET 请求。
回答by Navish Rampal
Use the following the Call the Action Link:
使用以下调用操作链接:
<%= Html.ActionLink("Click Here" , "ActionName","ContorllerName" )%>
For submitting the form values use:
要提交表单值,请使用:
<% using (Html.BeginForm("CustomerSearchResults", "Customer"))
{ %>
<input type="text" id="Name" />
<input type="submit" class="dASButton" value="Submit" />
<% } %>
It will submit the Data to Customer Controller and CustomerSearchResults Action.
它将数据提交给 Customer Controller 和 CustomerSearchResults Action。
回答by Alexey Smolyakov
Use this link inside Ajax.BeginForm
在 Ajax.BeginForm 中使用此链接
@Html.ActionLink(
"Save",
"SaveAction",
null,
null,
onclick = "$(this).parents('form').attr('action', $(this).attr('href'));$(this).parents('form').submit();return false;" })
;)
;)
回答by DJ_PLaying
My Solution to this issue is a fairly simple one. I have a page that does a customer search one by the whole email and the other by a partial, the partial pulls and displays a list the list has an action link that points to a actionresult called GetByID and passes in the id
我对这个问题的解决方案是一个相当简单的解决方案。我有一个页面,让客户通过整个电子邮件搜索一个,另一个通过部分搜索,部分拉出并显示一个列表,该列表有一个操作链接,指向名为 GetByID 的操作结果并传入 id
the GetByID pulls the data for the selected customer then returns
GetByID 提取所选客户的数据,然后返回
return View("Index", model);
which is the post method
这是post方法
回答by wayne.blackmon
This has been a difficult problem for me to solve. How can I build a dynamic link in razor and html that can call an action method and pass a value or values to a specific action method? I considered several options including a custom html helper. I just came up with a simple and elegant solution.
这对我来说是一个很难解决的问题。如何在 razor 和 html 中构建可以调用操作方法并将一个或多个值传递给特定操作方法的动态链接?我考虑了几个选项,包括自定义 html 帮助程序。我刚刚想出了一个简单而优雅的解决方案。
The view
风景
@model IEnumerable<MyMvcApp.Models.Product>
@using (Html.BeginForm()) {
<table>
<thead>
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
</tr>
</thead>
@foreach (Product p in Model.Products)
{
<tr>
<td><a href="@Url.Action("Edit", "Product", p)">@p.Name</a></td>
<td>@p.Price.ToString()</td>
<td>@p.Quantity.ToString()</td>
</tr>
}
</table>
}
The action method
动作方法
public ViewResult Edit(Product prod)
{
ContextDB contextDB = new ContextDB();
Product product = contextDB.Products.Single(p => p.ProductID == prod.ProductId);
product = prod;
contextDB.SaveChanges();
return View("Edit");
}
The point here is that Url.Action does not care whether the action method is a GET or a POST. It will access either type of method. You can pass your data to the action method using
这里的重点是 Url.Action 并不关心操作方法是 GET 还是 POST。它将访问任一类型的方法。您可以使用将数据传递给操作方法
@Url.Action(string actionName, string controllerName, object routeValues)
the routeValues object. I have tried this and it works. No, you are not technically doing a post or submitting the form but if the routeValues object contains your data, it doesnt matter if its a post or a get. You can use a particular action method signature to select the right method.
路由值对象。我试过这个,它的工作原理。不,从技术上讲,您不是在发帖或提交表单,但是如果 routeValues 对象包含您的数据,那么它是帖子还是获取都无关紧要。您可以使用特定的操作方法签名来选择正确的方法。

