简体   繁体   English

如何在Razor Page中检测回发?

[英]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() . 我正在使用一个具有form@functions{}部分的剃刀页面(cshtml)来捕获OnPost() All of that works fine. 所有这些都很好。

In the HTML section, I need to know when a post back has occurred and display a message. 在HTML部分中,我需要知道何时发生回发并显示一条消息。 I'm not sure where or how that is done in a Razor Page. 我不确定在“剃刀页面”中在何处或如何完成该操作。 I've tried IsPost but that isn't available. 我已经尝试过IsPost但是无法使用。

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 . 我在类中创建了一个字符串属性,并在OnPostOnPost设置了一个值,但是一旦进入视图,该属性OnPost null

I assigned a value into ViewData["mystring"] but it is also null once I get into the view. 我为ViewData["mystring"]分配了一个值,但是一旦进入视图,它也为null。

-- EDIT -- -编辑-
It seems the issue is that I'm trying to set a property in OnPost, which is the request. 看来问题是我正在尝试在OnPost(即请求)中设置属性。 After OnPost, OnGet will always fire. OnPost之后,OnGet将始终触发。 OnGet is the Response. OnGet是响应。 I can see the flip from POST to GET in this.Request.Method . 我可以在this.Request.Method看到从POSTGETthis.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. 您应该使用use Post-Redirect-Get模式。 The message can be stored in TempData 该消息可以存储在TempData中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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