代码之家  ›  专栏  ›  技术社区  ›  CoffeeCode

ASP.NET MVC ActionLink困难

  •  2
  • CoffeeCode  · 技术社区  · 16 年前

    我有一个操作链接

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

    这个动作有一个 [AcceptVerbs(HttpVerbs.Post)] Atttribute和LactionLink不起作用。

    如何使它通过“邮政”工作??

    4 回复  |  直到 15 年前
        1
  •  4
  •   LukLed    16 年前

    要发布到操作,我使用此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>
    
        2
  •  3
  •   ProfK    15 年前

    actionLink只是创建一个锚标记

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

    这本质上是一个get动词。要发布日志,必须将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>
    
        3
  •  2
  •   Community Mohan Dere    9 年前

    对不起,伙计,不能这样做:看看这里接受的回复 Does Html.ActionLink() post the form data? 在这里读一点关于标签的内容 http://www.w3schools.com/TAGS/tag_a.asp

        4
  •  0
  •   orip    16 年前

    另一个没有JS的选项是使用提交按钮而不是链接。这个按钮可以用CSS设计成任何样式。

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