0

I need to pass some data between two action results so I thought I'd try TempData (I'm really new to MVC so bear with me). This is what I've tried:

public ActionResult Edit(Companies companies, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null)
                    {
                        //Do stuff with file, save changes to database etc.

                        TempData["Companies"] = companies;

                        return RedirectToAction("bgcTest2", "Companies");

Now the temp data is stored. And to retrieve it:

public ActionResult bgcTest2(string BolagsID, Companies companies)
        {
            try
            {
                companies = TempData["Companies"] as Companies;
                int test = companies.BolagsID;

Now here's the problem; companies.BolagsID has a value but test is always null. Why? And how do I fix it?

EDIT:

I've noticed a rather curious thing; when trying Darin Dimitrovs suggestion, and probably when doing my try aswell, if I put a breakpoint only on the line which returns the null value it doesn't break until the line after. As if that line is commented out and doesn't execute at all. Welcome to the Twilight Zone.

EDIT2:

Take 2:

[HttpPost]
        public ActionResult Edit(Companies companies, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null)
                    {
                        // non related stuff

                        return RedirectToAction("bgcTest2", "Companies", new {BolagsID = companies.BolagsID});

And:

[Authorize]
        public ActionResult bgcTest2(int BolagsID)
        {
            try
            {
                int test = BolagsID;

I still get that "test does not exist in the current context".

EDIT3:

Take 2 works fine. My variables were null upon declaration because I never used them anywhere. I thought I'd take it one step at a time, but evidently, in some cases, you'll have to take a couple of steps to see it any of them works.

Lesson learned: Be sure to use declared variables, otherwise they will be null.

1 Answers1

3

I would recommend you not using TempData as it relies on the Session. If all you need is this BolagsID then simply pass it as parameter:

public ActionResult Edit(Companies companies, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        try
        {
            if (file != null)
            {
                //Do stuff with file, save changes to database etc.
                return RedirectToAction("bgcTest2", "Companies", new { BolagsID = companies.BolagsID });

and then:

public ActionResult bgcTest2(int bolagsID)
{
    ... use the bolagsID here directly
}

Unfortunately you cannot pass complex objects using this approach. But you could persist this object in your datastore and pass only the id to the target controller action that will allow you to retrieve it from this datastore after the redirect.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Using your example still gives me "The name 'test' does not exist in the current context" when watching it. :( – ChristopherSirén May 16 '13 at 06:26
  • Where did you see `test` in the code example I have posted in my answer? – Darin Dimitrov May 16 '13 at 06:34
  • Oh, sorry, where you wrote "... use the bolagsID here directly" I tried `int test = bolagsID;`, just to see if I would get a value. – ChristopherSirén May 16 '13 at 06:41
  • But the error message you mentioned cannot be related to this code: `"The name 'test' does not exist in the current context"`. I assume you are attempting to use this `test` variable in some other context. Sorry, without seeing your code it is hard to help. – Darin Dimitrov May 16 '13 at 06:44
  • Alright and what error message do you get? All you have shown is the declaration of the test variable here: `int test = BolagsID;`. But WHERE are you using it? You cannot possible get that error message on declaring a variable. You are getting it at the location where you are attempting to use it which I assume happens in some other part of the code you haven't shown. – Darin Dimitrov May 16 '13 at 06:55
  • I am such a newbie at this. I thought that declaring a variable should give it a value, but it seems that, in MVC, the variable will be `null` if it is never used. I had no idea. Oh, the shame. – ChristopherSirén May 16 '13 at 08:41
  • 1
    @ChristopherSirén, little rectification, it's not *in MVC* but *in C#* in general. – Darin Dimitrov May 16 '13 at 08:43