ASP.NET Razor 页面提交按钮不执行任何操作



>我有这个观点:

@page
@model TreesOnMars.Pages.NewGameModel
@{
ViewData["Title"] = "New Game";
}
<h2>New Game</h2>
<form method="POST">
<table>
<tr>
<td><label asp-for="Name"></label></td>
<td><input type="text" asp-for="Name" /></td>
</tr>
</table>
</form>
<button type="submit" asp-route-data="@Model.Name">Create Game</button>

使用此页面模型:

[Authorize]
public class NewGameModel : PageModel
{
public void OnGet()
{
}
public void OnPost()
{
Redirect($"Game/{Name}");
}
/// <summary>
/// The name of the game to create.
/// </summary>
public string Name { get; }
}

但是当我单击提交按钮时,没有任何反应 - OnGet 和 OnPost 方法都没有被调用。这是怎么回事?如何使提交按钮调用 OnPost((?

我遇到了同样的问题,只有在我添加了 returnUrl 参数后才能让它工作,不知道为什么:

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
if (ModelState.IsValid)
{
....
}
}
<form asp-route-returnUrl="@Model.ReturnUrl" method="post">
<button type="submit" class="btn btn-primary">Submit</button>
</form>

哎呀,我是个白痴 - 提交按钮需要在表单

最新更新