简体   繁体   English

ASP.NET Razor 页面提交按钮什么都不做

[英]ASP.NET Razor Pages submit button does nothing

I have this view:我有这样的看法:

@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>

With this page model:使用此页面 model:

[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; }
}

But when I click the submit button, nothing happens - neither the OnGet nor OnPost methods get called.但是,当我单击提交按钮时,什么也没有发生——OnGet 和 OnPost 方法都没有被调用。 What's going on here?这里发生了什么? How can I make the submit button call OnPost()?如何让提交按钮调用 OnPost()?

糟糕,我是个白痴-提交按钮必须位于表单内!

I got the same issue, only got it working once I added the returnUrl parameter, no idea why though:我遇到了同样的问题,只有在添加 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>

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM