簡體   English   中英

asp.net mvc actionlink困難

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM