Marco Oliveira
Marco Oliveira

Reputation: 71

Razor Page - Call method

Well, I'm doing a project using RAZOR PAGES and I have a question because I'm new at this. Whenever I want to call a method has to be within a form? For instance

I have a button out of a form, as I call a method that is

<button (herecallmethod) ></button>

public void Test(){

}

I need call method out of form

Upvotes: 1

Views: 2307

Answers (3)

Yiyi You
Yiyi You

Reputation: 18209

I want write a method in cshtml.cs and call method in cs.html out of form.

You can try to use <a></a>to call handler in cshtml.cs. Here is a demo:

cshtml:

<a asp-page-handler="Test">Test</a> 

cshtml.cs:

public void OnGetTest()
        {
            
        }

Upvotes: 2

tuncaycemuzun
tuncaycemuzun

Reputation: 1

There are two methods available. Either you would do it inside the form tag. You can investigate the use of @Html.BeginForm. Or you need to add 'onclick' property to the button and catch the event.

<button onclick='test()'>button text</button>

function test(){
alert('test')
}

Using @html.beginform : https://www.aspsnippets.com/Articles/ASPNet-MVC-HtmlBeginForm-Tutorial-with-example.aspx

Upvotes: 0

persian-theme
persian-theme

Reputation: 6638

To use the method inside the html code using the Razor engine you create a static class without a namespace. Then you can use it throughout the project.

or if it has a namespace, you should introduce its namespace at the top of the view.

Upvotes: 0

Related Questions