0

This is my View

<form method="post">
    <input type="text" />
    <button asp-page-handler="cancel">Cancel</button>
    <input type="submit" value="Save"/>
</form>

This is my controller

public async Task<IActionResult> OnPostCancelAsync(int? CaseID) {
   // Cancel code
}
public async Task<IActionResult> OnPostAsync() {
  // Submit code
}

Pressing the enter key when the cursor is in the textbox causes the Cancel code to be run. How do I tell it to run the default submit code instead?

Glyn
  • 316
  • 2
  • 20
  • You can use Javascript to set the default button when the Enter key is pressed. There are loads of examples on this site if you search. – Mike Brind Mar 12 '19 at 08:11
  • So there's no way from within Razorpages/.Net CORE/MVC to select the right action? – Glyn Mar 12 '19 at 09:12
  • The issue is not related to Razor Pages/.NET Core. It's how default actions in web pages are decided. You can also change the order in which the buttons appear. – Mike Brind Mar 12 '19 at 09:50
  • Razor pages have the ability to have multiple handlers for post requests, so, stupidly, I thought that there'd be some way built into the framework to deal with selecting the default action, rather than having to go outside and hack something else in. – Glyn Mar 12 '19 at 10:00
  • What happens when someone hits Enter while focus is in a text input is decided by the browser. Changing the order of the buttons appears to give you the desired behaviour, but it might not work in different browsers. See https://stackoverflow.com/questions/925334/how-is-the-default-submit-button-on-an-html-form-determined – Mike Brind Mar 12 '19 at 10:12
  • Figured it'd be a JavaScript hack I'd end up using, just hoped it'd be something included in the framework. – Glyn Mar 12 '19 at 11:08

0 Answers0