我有一個ActionLink的在我看來asp.net的MVC ActionLink的困難
<%= Html.ActionLink("action","controller") %>
的操作有一個[AcceptVerbs(HttpVerbs.Post)]
atttribute和lactionlink不起作用。
如何使它通過「POST」?
我有一個ActionLink的在我看來asp.net的MVC ActionLink的困難
<%= Html.ActionLink("action","controller") %>
的操作有一個[AcceptVerbs(HttpVerbs.Post)]
atttribute和lactionlink不起作用。
如何使它通過「POST」?
要發佈一個動作我用這個JavaScript函數:
function postToUrl(path, params, method)
{
method = method || "post"; // Set method to post by default, if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for (var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form); // Not entirely sure if this is necessary
form.submit();
}
它創建HTML表單,所以您不必在視圖代碼來創建它。這樣你可以使用:
<button onclick="postToUrl('/Controller/Action');">Link</button>
對不起隊友,不能做:看看這裏接受回覆Does Html.ActionLink() post the form data?和閱讀一些關於這裏http://www.w3schools.com/TAGS/tag_a.asp
標籤的ActionLink的只是創建了一個錨標記
<a href="url">link text</a>
這本質上是一個GET動詞。要做一個POST,你必須將actionlink包裝在一個form標籤中,並且你可以用一些jQuery來覆蓋click功能。
<% using (Html.BeginForm("action","controller"))
{ %>
<%= Html.ActionLink("Link Text", "action","controller") %>
<% } %>
<script>
$(document).ready(function() {
$("a").click(function() {
$(this).parents('form').submit();
return false;
});
});
</script>
+1,但是您可能希望使用'$(this).closest('form')'而不是'.parents'? – orip 2010-02-16 00:42:21
沒有JS的另一種選擇是使用提交按鈕而不是鏈接。該按鈕可以用CSS設置樣式。
<%using (Html.BeginForm("action", "controller") {%>
<button type="submit">text</button>
<%}%>
你有POST通過