开发者

Problem showing a messaje to the user using ASP MVC TempData

开发者 https://www.devze.com 2023-03-19 03:23 出处:网络
I\'m using TempData to show a message to the user. I put a string in the TempData and later I read the string, and if it is not empty, then I show a DIV that contain the message.

I'm using TempData to show a message to the user. I put a string in the TempData and later I read the string, and if it is not empty, then I show a DIV that contain the message.

All works fine, and if the user refresh the page the message are not shown (Thats what I want). The problem is that if the user navigate to other p开发者_如何学Goage and then press the back button in the browser, then the message are shown again, and I do not want this.

What could I do to avoid this behaviour?

Thanks.

This is the code I use to read the TempData (Razor + VB). There is a DIV #commonMessage, with this code I put the string inside the div and show it. As I said before, it's working, but the only problem is that the TempData is still there if the user click back in the browser.

 @If Not IsNothing(TempData("MessageUser")) AndAlso TempData("MessageUser") <> String.Empty Then
        Dim str As String = TempData("MessageUser")
        @<script type="text/javascript">
             $(document).ready(function () {
                 $('#commonMessage').html("@str");
                 $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
             })
        </script>
    End If

EDIT: Seems like the TempData are being catched, because if I Disable the cache for the action where I'm showing the message (Using the Attribute System.Web.Mvc.OutputCache(NoStore:=True, Duration:=0, VaryByParam:="*")) the problem dissapears. But It would be better I we could find a method that not involve disabling the cache...

REQUESTED EDIT: I'm very newby in ASP, so I try to clarify what i'm triying to do. When an user performs an action (edit a client, for example), I redirect the client to the client list page, and I show a message that tell to the user "The client data was update susessfully". I'm triying to do it in a way that makes the message to be show only once. Maybe the TempData is not the right way (I don't know, 'cos i'm learning yet), but the target is to show a message to an user only once (no matter if the urser refresh or if the user go to other page and then press back in the browser)... using TempData or using something more adequate to our purpose.


Essentially, you are wanting TempData to do what you want, rather than using the right tool for what you want.

TempData is, by design, intended to be used for caching data across HTTP redirections. That is what it exists for. It is not clear from your post if this is the scenario that you are using.

Ie:

Page redirection, with data in TempData, that is then displayed to the user. Refresh the page you have arrived on, and the TempData is no longer there (there has been no redirection, just a refresh).

If the user then navigates to another page, then uses the back button, the browser will have cached the html of your page and will redisplay that. That is the correct behaviour.

I also think that in your testing, you are getting it wrong. Ie, by disabling the caching, you are just knocking out TempData altogether and you will not get the correct behaviour. Ie, the message will NEVER appear, not just when you hit the back button.

Your jQuery looks inefficient. You are making it do things it doesn't need to do. You could use razor to populate your div with your message. Set the div to not display, ie:

<div id="commonMessage" style="display:none;">

Then use jQuery to show it:

$('#commonMessage').show();

Your post isn't that clear, but in summary, I would say you are seeing what you should.

Maybe you should describe, in an Edit, what you want your app to do. That way it would be easier to answer. As things stand, you have told us what happens and what you put in your view, but it is not clear what you expect.

You should also understand TempData better: it only persists between Controller actions, ie, when a redirect occurs. It stores its data in the Session store, which I believe is affected by the caching attribute you mention.

0

精彩评论

暂无评论...
验证码 换一张
取 消