How do I navigate to a new page with a Button in ASP.net Core?

19,362

Solution 1

This should work:

<a asp-action="Create"><button class="btn btn-primary">Create</button></a>

Solution 2

Since you mentioned you want to navigate to a new page I'd use a different controller for it that has the Create action (or just an Index action). You can nest an input inside an anchor tag.

<a asp-controller="DifferentControllerName" asp-action="Create">
    <input type="button" class="btn btn-primary" value="Create"/>
</a>

It renders like this:

<a href="/DifferentControllerName/Create">
    <input type="button" class="btn btn-primary" value="Create">
</a>
Share:
19,362
Adam Wells
Author by

Adam Wells

Excitable, talented coder with a long history of doing the impossible. Also, i play a TON of board games. My personal project right now is an NFC-Tag driven application for managing large LARP events and automating some parts of Game Mastering using state-based tracking and live mobile notifications.

Updated on June 25, 2022

Comments

  • Adam Wells
    Adam Wells almost 2 years

    I've got a webpage that currently links to another page like this:

    <a asp-action="Create">Create New</a>
    

    I've tried this and it just sat on the page very happily, but didn't take me where I want to go:

    <button asp-action="Create" class="btn btn-primary">Create</button>
    

    How can I change this to use a button instead of a text link?