简体   繁体   English

asp.net mvc actionlink困难

[英]asp.net mvc actionlink difficulties

i have an actionlink in my view 我认为我有一个actionlink

<%= Html.ActionLink("action","controller") %>

the action has a [AcceptVerbs(HttpVerbs.Post)] atttribute, and the lactionlink doesn't work. 该动作有一个[AcceptVerbs(HttpVerbs.Post)] atttribute,并且lactionlink不起作用。

how to make it work by "POST"?? 如何让它通过“POST”工作?

To post to an action I use this JavaScript function: 要发布到动作,我使用此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();
}

It creates html form, so you don't have to create it in view code. 它创建了html表单,因此您不必在视图代码中创建它。 this way you can use: 这样你可以使用:

<button onclick="postToUrl('/Controller/Action');">Link</button>

an ActionLink is just creates an anchor tag ActionLink只是创建一个锚标记

<a href="url">link text</a>

This is inherently a GET verb. 这本质上是一个GET动词。 To do a POST, you must wrap the actionlink inside a form tag and you can override the click function with some jQuery. 要执行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>

Sorry mate, can't be done: take a look at the accepted reply here Does Html.ActionLink() post the form data? 对不起伙伴,无法完成:看看这里接受的回复Html.ActionLink()发布表单数据吗? and read a bit about the tag here http://www.w3schools.com/TAGS/tag_a.asp 并在这里阅读一些关于标签的内容http://www.w3schools.com/TAGS/tag_a.asp

Another option with no JS is to use a submit button instead of a link. 没有JS的另一个选择是使用提交按钮而不是链接。 The button can be styled into anything with CSS. 可以使用CSS将按钮设置为任何样式。

<%using (Html.BeginForm("action", "controller") {%>
  <button type="submit">text</button>
<%}%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM