How to detect postback in Razor Page?

烈酒焚心 提交于 2019-12-24 20:13:11

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/47820332/how-to-detect-postback-in-razor-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!