[asp.net-mvc] Using Tempdata in ASP.NET MVC - Best practice

I am using ASP.NET MVC 3 to build a web application.

What I am trying to do is pass values between two controllers, though there are many ways to do this I am particular interested in using TempData for this.

public ActionResult Action1()
{
    string someMessage;
    Test obj = SomeOperation();
    if(obj.Valid)
    {
        someMessage = obj.UserName;
    }
    else
    {
        someMessage = obj.ModeratorName;
    }

    TempData["message"] = someMessage;

    return RedirectToAction("Index");
}

public ActionResult Index()
{
    ViewBag.Message = TempData["message"]

    return View();
}

So is the use of TempData here correct ? I mean under best programming practices is this correct way of using TempData ?

In what real time cases should TempData be used ?

Note : I have gone through the following links

Thanks

This question is related to asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 tempdata

The answer is


Just be aware of TempData persistence, it's a bit tricky. For example if you even simply read TempData inside the current request, it would be removed and consequently you don't have it for the next request. Instead, you can use Peek method. I would recommend reading this cool article:

MVC Tempdata , Peek and Keep confusion


Please note that MVC 3 onwards the persistence behavior of TempData has changed, now the value in TempData is persisted until it is read, and not just for the next request.

The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request. https://msdn.microsoft.com/en-in/library/dd394711%28v=vs.100%29.aspx


Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core

Examples related to asp.net-mvc-3

Better solution without exluding fields from Binding IIs Error: Application Codebehind=“Global.asax.cs” Inherits=“nadeem.MvcApplication” Can we pass model as a parameter in RedirectToAction? return error message with actionResult Why is this error, 'Sequence contains no elements', happening? Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function 500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid String MinLength and MaxLength validation don't work (asp.net mvc) How to set the value of a hidden field from a controller in mvc How to set a CheckBox by default Checked in ASP.Net MVC

Examples related to asp.net-mvc-2

Better solution without exluding fields from Binding How to set the value of a hidden field from a controller in mvc Making a Simple Ajax call to controller in asp.net mvc ASP.Net MVC - Read File from HttpPostedFileBase without save Session state can only be used when enableSessionState is set to true either in a configuration Using Tempdata in ASP.NET MVC - Best practice Getting index value on razor foreach Current date and time - Default in MVC razor Url.Action parameters? How can I get all element values from Request.Form without specifying exactly which one with .GetValues("ElementIdName")

Examples related to tempdata

TempData keep() vs peek() Using Tempdata in ASP.NET MVC - Best practice