gpt4 book ai didi

.net - asp.net mvc actionlink 困难

转载 作者:行者123 更新时间:2023-12-02 06:44:28 24 4
gpt4 key购买 nike

我有一个 Action 链接在我看来

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

该操作具有 [AcceptVerbs(HttpVerbs.Post)] 属性,并且 lactionlink 不起作用。

如何通过“POST”使其工作??

最佳答案

要发布到一个 Action ,我使用这个 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 表单,因此您不必在 View 代码中创建它。这样你就可以使用:

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

关于.net - asp.net mvc actionlink 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2269822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com