Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect postback in Razor Page?

I'm using a single razor page (cshtml) that has a form and @functions{} section to capture an OnPost(). All of that works fine.

In the HTML section, I need to know when a post back has occurred and display a message. I'm not sure where or how that is done in a Razor Page. I've tried IsPost but that isn't available.

I created a string property in the class and set a value on it in OnPost but once I'm in the view, the property is null.

I assigned a value into ViewData["mystring"] but it is also null once I get into the view.

-- EDIT --
It seems the issue is that I'm trying to set a property in OnPost, which is the request. After OnPost, OnGet will always fire. OnGet is the Response. I can see the flip from POST to GET in this.Request.Method. So I lose any indication that a postback happened.

Any suggestions on how that is done?

like image 951
4thSpace Avatar asked Oct 24 '25 16:10

4thSpace


1 Answers

You should use use Post-Redirect-Get pattern. The message can be stored in TempData

like image 99
Albert Avatar answered Oct 26 '25 06:10

Albert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!