G
G
Grigory Vashkevich2014-03-22 22:17:02
ASP.NET
Grigory Vashkevich, 2014-03-22 22:17:02

How to pass data between asp.net mvc controllers?

There are 2 methods, generally located in different controllers:

public ActionResult FirstMethod()
{
    ...
    ViewBag.Message = "My message";
    return RedirectToAction("SecondMethod");
}

and
public ActionResult SecondMethod()
{
    ...
    return View();
}

How can I make the "SecondMethod" method view display the ViewBag.Message value?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Vashkevich, 2014-03-23
@konar

Solved the problem by using the TempData property.

public ActionResult FirstMethod()
{
    ...
    TempData["Message"] = "My message";
    return RedirectToAction("SecondMethod");
}

public ActionResult SecondMethod()
{
    ...
    ViewBag.Message = TempData["Message"];    
    return View();
}

K
Kokcuk, 2014-03-23
@Kokcuk

There is no studio at hand to check, but try calling something like
return View("~/Views/SecondControllerName/MyView") in the first controller with the
full path

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question