[英]asp.net mvc actionlink difficulties
我认为我有一个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>
ActionLink只是创建一个锚标记
<a href="url">link text</a>
这本质上是一个GET动词。 要执行POST,您必须将actionlink包装在表单标记内,并且可以使用某些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>
对不起伙伴,无法完成:看看这里接受的回复Html.ActionLink()发布表单数据吗? 并在这里阅读一些关于标签的内容http://www.w3schools.com/TAGS/tag_a.asp
没有JS的另一个选择是使用提交按钮而不是链接。 可以使用CSS将按钮设置为任何样式。
<%using (Html.BeginForm("action", "controller") {%>
<button type="submit">text</button>
<%}%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.