Search code examples
c#razorblazormodbusplc

Razor Pages, button that permits to execute code without refreshing the page


I'm trying to create a web app that connects to a plc as i want to access it with my phone. I was able to connect to it using modbus rtu and i can write single registers without any problem. The web app conists on a single page that has some buttons. At the moment just i'm trying to light a led and it's working. The problem is that whenever i press a button the page refreshes and i don't want that. I want to execute the code without the page refreshing.

In the .cshtml i'm using a <form method="post" ...> and inside i have a <input type="submit" .... asp-page-handler="lightOn". In the .cshtml.cs i'm using a simple public void OnPostlightOn() and whenever i press the button, the led lights up but the page refreshes.

I tried to use a type="button" but the OnPost method doesn't work and i don't know why, i'm new to c# and razor pages.

Is there any way to prevent the refresh when i press the button?

-----UPDATE------ I actually tried Blazor, as suggested in the comments, and i was able to achieve what i wanted without dealing directly with js.


Solution

  • submitting a form does cause page refresh unless using xmlhttprequest (ajax) or blazor where things happen more magically