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

  • Home
  • SEARCH
  • 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 7822369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:54:34+00:00 2026-06-02T07:54:34+00:00

I have an action called Messages which has two forms. The first form posts

  • 0

I have an action called Messages which has two forms.

The first form posts to SendMessage and the second posts to MessagesDelete. Both pages return a ReturnToAction of the Messages view, and both use TempData to return feedback data (message sent, messages deleted etc) indicating that an action has occured.

MessagesDelete works fine, returning the TempData just the first time and then removing it in future requests. SendMessage on the other hand, persists the TempData endlessly.

Here’s some simplified code.

    [Authorize]
    public ActionResult Messages(int? id, string message)
    {
        MessagesModel model = new MessagesModel();
        // build model data here
        return View(model);
    }

    [Authorize]
    [HttpPost]
    public ActionResult MessagesDelete(int[] selectedObjects, int? id)
    {
        // delete objects
        TempData["MessagesDeleted"] = selectedObjects.Count() + " deleted";
        return RedirectToAction("Messages", new { id = id });
    }

    [Authorize]
    [HttpPost]
    public ActionResult SendMessage(SendMessageModel model)
    {
        // send my message
        TempData["MessageSent"] = "message sent!";
        return RedirectToAction(model.action, new { id = model.action_id } ); 
    }

The only difference I can see is that MessagesDelete is send the id directly, whereas SendMessage has it as a property within the model.

  • 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-06-02T07:54:35+00:00Added an answer on June 2, 2026 at 7:54 am

    It looks like you’re trying to implement one-off messages to users based on actions they take. In this situation, I would rathe architect your solution a little bit differently than take on the headache that is using TempData and it’s unreliability.

    Here’s what I would do:

    1) Create a base ViewModel for all ViewModels expecting to show one-off messages.

    public class OneOffMessageViewModelBase
    {
        public string Message { get; set; }
    }
    

    2) Then, add a OnResultExecutingFilter to pull data from session to store in the view model (or, add an override to your base controller):

    public class AllowOneOffMessagingAttribute : ActionFilterAttribute
    {
        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
             base.OnResultExecuting(filterContext);
    
             var actionResult = filterContext.Result as ViewResult;
    
             if(actionResult != null)
             {
                  var viewModel = actionResult.ViewData.Model as OneOffMessageViewModelBase;
    
                  if(viewModel != null)
                  {
                       if(Session["OneOffMessage"] != null)
                       {
                           viewModel.Message = Session["OneOffMessage"];
                           Session["OneOffMessage"] = null;
                       }
                  }
             }
        }
    }
    

    3) Then, you simply need to assign to that session variable, and you can be assured that the next view (and not partial view) will assign the one off message to the view model, and then the data will be deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 3 application which has a post action called Create
Projects have many tasks and a task has a custom RESTful action called 'approve'.
I have a class that has an Action<string> called DisplayData ; In various points
I have a form rendered in the view which action is setted to the
So I have a controller called Music with one action which is index. On
I have a model called posts, and it has many attachments. The attachments model
I have an action called List that shows the results of a search. It
I have an action method VerifyNewUser() that gets called when the user clicks a
I have a action on my controller (controller name is 'makemagic') called 'dosomething' that
I have code that I want to be executed before every action is called.

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.