ASP.NET TempData persists between requests

前端 未结 2 770
[愿得一人]
[愿得一人] 2020-12-11 16:52

I am using temp data as follow in my controllers - very simple, when there is a problem:

TempData(\"StatusMessage\") = \"You have no items set to Auto-Ship.\         


        
相关标签:
2条回答
  • 2020-12-11 17:02

    The sole purpose of TempData is to persist until the next request. Stuff you do not want to persist until the next request should go into ViewData, instead.

    Realistically, this means that TempData is only safe to use when redirecting. When not redirecting, the "next request" could be anything.

    0 讨论(0)
  • 2020-12-11 17:02

    would this be acceptable (removing the error once it has been shown):

    <%  If TempData.ContainsKey("ErrorMessage") Then %>
    <script> $('div.error-container').show();</script>
    <div class="msg-error"><p><%=TempData("ErrorMessage") %></p></div>
    <% 
        TempData.Remove("ErrorMessage")
    End If
    %>
    
    0 讨论(0)
提交回复
热议问题