如何在Razor Page中检测回发? [英] How to detect postback in Razor Page?
问题描述
我正在使用一个剃刀页面(cshtml),该页面具有form
和@functions{}
部分以捕获OnPost()
.所有这些都很好.
I'm using a single razor page (cshtml) that has a form
and @functions{}
section to capture an OnPost()
. All of that works fine.
在HTML部分中,我需要知道何时发回邮件并显示一条消息.我不确定在剃刀页面"中在何处或如何完成该操作.我已经尝试过IsPost
,但该功能不可用.
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.
我在类中创建了一个字符串属性,并在OnPost
中为其设置了一个值,但是一旦进入视图,该属性就是null
.
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
.
我在ViewData["mystring"]
中分配了一个值,但是一旦进入视图,该值也将为空.
I assigned a value into ViewData["mystring"]
but it is also null once I get into the view.
-编辑-
看来问题是我正在尝试在OnPost(即请求)中设置属性.在OnPost之后,OnGet将始终触发. OnGet是响应.我可以在this.Request.Method
中看到从POST
到GET
的翻转.因此,我没有任何迹象表明发生了回发.
-- 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
这篇关于如何在Razor Page中检测回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!