TempData ["message"] is not reliable - what am I doing wrong?

I use TempDate["Message"]to display small update banners as the user does things on my site like this:

[AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admins")]
public ActionResult Delete(int id)
{
    _Repo.DeletePage(id); // soft-delete

    TempData["Message"] = "Page deleted!";
    return RedirectToAction("Revisions", "Page", new { id = id });
}

Then on my main page I have the following:

<%-- message box (show it only if it contains a message) --%>
<% string Message = (TempData["Message"] ?? ViewData["Message"]) as string; 

   if(!string.IsNullOrEmpty(Message)){
       %>
       <div id="message"><%:Message %></div>
   <% }

   TempData["Message"] = null; ViewData["Message"] = null; %>

I hit both TempData and ViewData because I read somewhere that TempData should be used for redirection, and ViewData should be used differently.

Problem: Often a message does not appear immediately. Sometimes to display a message requires a few clicks on two parts of the site. It is very strange.

Any ideas?

+3
source share
5 answers

, TempData["Message"] . ASP.NET MVC , TempData ? TempData["Message"] (. http://forums.asp.net/p/1528070/3694325.aspx). uage TempData["Message"], TempData["Message"] TempDataDictionary.

, TempData["Message"] Revisions Page, .

+2

TempData , ViewData. , TempData ...

TempData - (, Revisions TempData["Message"]).

PRG MVC- (Post-Redirect-Get), Get. , Get, , , , TempData:

public ActionResult System() {
   SystemAdminVM model = (SystemAdminVM)TempData["screenData"] ?? new SystemAdminVM();

; , TempData ViewData . , , ...

!

+2

- , , TempData, ViewData.

, TempData , . ViewData.

0

If you have configured several workflows for your application, but the session state mode is "InProc", then you cannot use the default implementation of TempData, because the session state becomes unusable. (see ASP.NET session state and several workflows )

Instead, you can use the MvcFutures CookieTempDataProvider .

0
source

Source: https://habr.com/ru/post/1747241/


All Articles