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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

3 Comments

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.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.