我想从 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/