2

I am trying to pass some value from one controller to another something like

TempData["data"]="data";

but it says Tempdata does not exist in current context.So i decided to use ViewBag and surprisingly, i get the same error message for ViewBag.

I read about it and it has something to do with BaseController.So I really have to derive from a basecontroller to make it work?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
RelatedRhymes
  • 428
  • 6
  • 26

1 Answers1

0

Your controller must be derived from BaseController
You can use TempData[""] to pass message from one controller to another. Here is a code which will help you:

namespace namespaceName.Controllers
{
    public class ControllerName : Controller
    {
        public ActionResult firstController()
        {
            var data = TempData["data"].ToString();
        }
        public ActionResult secondController()
        {
            TempData["data"] = "data";
            return RedirectToAction("firstController");
        }
    }
}

the TempData["data"] = "data" will be passed to firstController from secondController
Hope this helps

Brien Foss
  • 3,336
  • 3
  • 21
  • 31
anand
  • 1,559
  • 5
  • 21
  • 45