3

I'vm been trying to get a site running using ASP.NET MVC 3 and I came across the new dynamic ViewModel. It's great to pass values quickly to the view without using "magic strings". I'm wondering if there's something similar for the TempData that keeps it's values after a RedirectToAction.

Thanks.

Carles Company
  • 7,118
  • 5
  • 49
  • 75

2 Answers2

4

TempData is not dynamic in MVC 3 (as long as I can tell anyway) e.g. this syntax does not compile :

TempData.Account = "Geronimo"

because Account property/field does not exists on the type.

ViewBag Is dynamic

ViewBag.Acount = "Geronimo" 

compiles.

agf
  • 171,228
  • 44
  • 289
  • 238
Olivier B
  • 41
  • 2
2

You can enable the session state as was used in web forms and use that to store the data if you want, is this something you would be interested in? Just google "session state in asp.net mvc"

When you do this...

TempData("test") = "cool string"

You can access is later on using tempdata.test (though they aren't sure if they are going to keep it as tempdata or going to change it).

Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • I know I can use sessions. But the good thing about TempData is that it's automatically destroyed after the request. I wanted to know if there was something similar to the new dynamic ViewModel but for TempData. – Carles Company Nov 29 '10 at 07:19
  • TempData is now dynamic, as far as I am aware and works in much the same way as the dynamic view model. You are better of storing thhhings in the view model if you are able to, as a result, I never end up using temp data except for when doing sample mvc code or watching mvc tutoring videos as they always use temp data in samples – Erx_VB.NExT.Coder Dec 01 '10 at 08:07
  • Regardless of where TempData is stored, keep in mind that after reading the value out, it's deleted immediately. Doing this var x = TempData["test"] would mean the value would be lost for any subsequent requests. – Razor May 30 '11 at 14:03