Is using TempData to pass information between controller actions a bad practice?

I have certain situations where I need to pass a value between controller actions.

  • When passing returnUrl from the view to all nested views . in I have

    @{ TempData["returnURL"] = Request.Url.AbsoluteUri; } 

    and then access it in a similar way (in my real version I make sure the key is in TempData and that returnURL is the real URL):

     return Redirect(TempData["returnURL"].ToString()); 

    If it is necessary to continue the first page change (i.e. search the page β†’ Edit page β†’ Section edit page) I add it again

     TempData["returnURL"] = TempData["returnURL"]; 
  • When I need to pass a value from one controller action by looking at another controller action called by ajax, for example, here:

     public ViewResult Index(FormCollection form) { var model = new GridColumnChooserViewModel(); //Select deleted/not deleted rows if (form.HasKeys()) model.ShowRows = (form["deletedDropDown"] == null) ? "Active" : GetOptionByName(form["deletedDropDown"]); TempData["ShowRows"] = model.ShowRows; ... } 

    and then in my other ajax-called action controller, I access it:

     public JsonResult GetData() { //Select deleted/not deleted rows var showRows = (TempData.ContainsKey("ShowRows") && TempData["ShowRows"] == null) ? "Active" : GetOptionByName(TempData["ShowRows"].ToString()); //refresh tempdata showrows so it is there for next call TempData["ShowRows"] = model.ShowRows; return this.GetDataSource(showRows); } 

My question is, is this really bad practice? In my understanding of this, I am using TempData as a session cookie. Is there a better way to do this, for example using an actual cookie?

+4
source share
3 answers

Yes, I would say that this is generally bad practice. Although the ViewData dictionary approach is quick and fairly easy to implement, it can lead to typos and errors that do not appear at compile time. An alternative would be to use the ViewModel template, which allows you to use strongly typed classes for the specific view that you need to display values ​​or content inside. It ultimately gives you a safe type and compile time check with intellisense.

My first choice would be to use a presentation model. If this is not appropriate, using session state can be very good.

+1
source

It seems you are using TempData to stream through the various pages of your site; in general, I would say that this is bad practice.

Ideally, you will pass all the upcoming state that you need to the client, and the client will save it (in some JSON or something else). Then the client will return it to you as part of its action, and then you will return the corresponding state, etc .; this speaks more about the statelessness of HTTP applications.

+1
source

I changed both situations to use Session so that I didn't have to keep pushing the TempData value.

 public ActionResult Create() { Session["returnURL"] = Request.UrlReferrer.AbsoluteUri; ... } 

Then I refer to it as follows

 var returnURL = (Session["returnURL"] != null) ? Session["returnURL"].ToString() : Url.Action("Index", "Home"); 

Seems a little better.

0
source

All Articles