'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:

[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. What's going on here? How can I make the submit button call OnPost()?



Solution 1:[1]

I got the same issue, only got it working once I added the returnUrl parameter, no idea why though:

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>

    

Solution 2:[2]

Oops, I'm an idiot - the submit button needs to be inside the form!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josua Wilson
Solution 2 ekolis