Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top