gpt4 book ai didi

c# - 如何直接从 Razor View 调用 .NET CORE MVC 中的 POST 方法?

转载 作者:行者123 更新时间:2023-12-01 21:55:22 25 4
gpt4 key购买 nike

我想从 Controller 调用我的方法:

[HttpPost]
public async Task<IActionResult> SendPhoneVerificationCode()
{
// ...code
return RedirectToAction(nameof(ConfirmPhoneNumber));
}

使用按钮从我的 Index.cshtml View

<button class="btn btn-primary" name="button">Send Verification Phone Code</button>

我该怎么做?

最佳答案

你的代码应该是

@page // important

<form method="POST">
<input type="submit" asp-page-handler="SendPhoneVerificationCode" value="Submit" />
</form>

在你的代码后面。

public IActionResult SendPhoneVerificationCode() 
{
return RedirectToPage("./ConfirmPhoneNumber");
}

我建议你使用 razor 页面而不与 mvc Controller 混合

您可以阅读更多here

关于c# - 如何直接从 Razor View 调用 .NET CORE MVC 中的 POST 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58015982/

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