Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:17:39+00:00 2026-05-24T23:17:39+00:00

I want to use TempData to store messages between Post and followed redirect but

  • 0

I want to use TempData to store messages between Post and followed redirect but TempData are always empty.

I have BaseContoller offering some infrastructure for passing TempData. Simplified code looks like:

public abstract class BaseController : Controller
{
  public const string AuditMessagesKey = "AuditMessages";

  private List<InformationMessage> _informationMessages = new List<InformationMessage>();

  protected BaseController()
  {
    // I also tried this in overriden Initialize
    ViewData[AuditMessagesKey] = GetAuditMessages();
  }

  protected void AddAuditMessage(InformationMessage message)
  {
    if (message == null)
      return;

     _informationMessages.Add(message);
  }

  protected override void OnResultExecuting(ResultExecutingContext filterContext)
  {
    base.OnResultExecuting(filterContext);

    if (filterContext.Result is RedirectToRouteResult)
    {
      // I see that messages are stored into TempData
      TempData[AuditMessagesKey] = _informationMessages;
      // This also doesn't help
      // TempData.Keep(AuditMessagesKey);
    }
  }

  private ICollection<InformationMessage> GetAuditMessages()
  {
    // TempData are always empty here
    var messages = TempData[AuditMessagesKey] as List<InformationMessage>;

    if (messages == null)
    {
      messages = new List<InformationMessage>();
    }

    return messages;
  }
}

Action method looks like:

  [HttpPost]
  public ActionResult CancelEdit(RequestSaveModel model)
  {
    AddAuditMessage(new InformationMessage
      {
        Message = String.Format(Messages.RequestEditationCanceled, model.Title),
        Severity = MessageSeverity.Information
      });

    return RedirectToAction("Detail", new { Id = model.Id});
  }

Application is tested on VS Development web server. There are no Ajax calls and I removed all Html.RenderAction calls from my master page. I can see that TempData are accessed only once per request in GetAuditedMessages and stored only once in OnResultExecuting. Nothing overwrites the data. Session state is allowed.

The code is little bit simplified. We are also using antiforgery token, custom filters for authorization and for action selection but it should not affect TempData behavior.

I don’t understand it. I used TempData before in test application and it worked fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:17:41+00:00Added an answer on May 24, 2026 at 11:17 pm

    The problem I see in your code is that you are trying to retrieve the data from TempData in the controller’s constructor – which is before it is available.

    Move the call to GetAuditMessages() into an OnActionExecuting method, and it will be accessible.

    public abstract class BaseController : Controller
    {
      public const string AuditMessagesKey = "AuditMessages";
    
      private List<InformationMessage> _informationMessages = new List<InformationMessage>();
    
      protected BaseController()
      {
        // TempData is not available yet
      }
    
      protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          ViewData[AuditMessagesKey] = GetAuditMessages();
    
          base.OnActionExecuting(filterContext);
      }
    
      protected void AddAuditMessage(InformationMessage message)
      {
        if (message == null)
          return;
    
         _informationMessages.Add(message);
      }
    
      protected override void OnResultExecuting(ResultExecutingContext filterContext)
      {
        base.OnResultExecuting(filterContext);
    
        if (filterContext.Result is RedirectToRouteResult)
        {
          // I see that messages are stored into TempData
          TempData[AuditMessagesKey] = _informationMessages;
          // This also doesn't help
          // TempData.Keep(AuditMessagesKey);
        }
      }
    
      private ICollection<InformationMessage> GetAuditMessages()
      {
        var messages = TempData[AuditMessagesKey] as List<InformationMessage>;
    
        if (messages == null)
        {
          messages = new List<InformationMessage>();
        }
    
        return messages;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use ORDER BY RAND() query mysql. But I have some question want
I want use BYTE_ORDER macro in my Xcode project but i can't because i
i want use some data from a website with web service. i have a
I want use JQuery mobile for the front-end of my mobile application, but I
We have a powerbuilder application and we want use a scanner through this application
When to use ViewBag, ViewData, or TempData in view. In the controller i want
I want use a typedef struct that isn't already defined, but it is later.
I want use the Scintilla controls and the RadControls for Winforms, but I do
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
I want use Kendo ui window, but I can't see what the problem is.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.