Home > Enterprise >  Razor pages button on click event
Razor pages button on click event

Time:12-07

How can I link a button click to a method on Razor pages, am using ASP.NET Core 6?

I have this bit of code from a razor pages Test.cshtml

<Button>Do some stuff</Button>

and I want to link it to this method from Test.cshtml.cs

public void SomeStuff() { //Some C# code }

I know how to do it if it was a post form but is there any other way to invoke that method?

Trying to link a button click event from a cshtml razor pagee to a function in a cshtml.cs file

CodePudding user response:

Trying to link a button click event from a cshtml razor pagee to a function in a cshtml.cs file

Can you use this way ?

Wrap the anchor tag element in the button element and is able to navigate to the specified page method when the button was clicked.

<button ><a asp-page-handler="SomeStuff" >Do some stuff</a></button>

Index.cshtml.cs:

public  void OnGetSomeStuff() { }

CodePudding user response:

can you try this way ?

<Button id="ButtonID">Do some stuff</Button>

<script>
$("#ButtonID").click(function() {
CallAjaxMethod("/Test/SomeStuff/").then(function (result) {
       // Your action after getting the response
    });
});
</script>
  • Related