简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM