MVC - Ways to Pass Data Between Controllers


To share data between different controllers in an ASP.NET MVC application, you can use the following methods.

1. Using TempData

TempData is useful for transferring small amounts of data between controllers, especially during redirects. It persists data only for one subsequent request.

Example

public class HomeController : Controller
{
    public ActionResult Index()
    {
        TempData["Message"] = "Data from HomeController";
        return RedirectToAction("About", "Other");
    }
}

public class OtherController : Controller
{
    public ActionResult About()
    {
        var message = TempData["Message"]; // Retrieve the data
        return View();
    }
}

2. Using Session

Session allows sharing data across requests for a single user session. It's ideal for larger data that needs to persist across multiple controllers.

Example

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Session["SharedData"] = "Data shared via Session";
        return RedirectToAction("About", "Other");
    }
}

public class OtherController : Controller
{
    public ActionResult About()
    {
        var sharedData = Session["SharedData"]; // Retrieve the data
        return View();
    }
}

3. Using Query Strings

Pass data between controllers by including it in the query string of the URL.

Example

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("About", "Other", new { message = "Data via Query String" });
    }
}

public class OtherController : Controller
{
    public ActionResult About(string message)
    {
        // Retrieve the data from the query string
        return View((object)message);
    }
}

4. Using Route Values

Pass data via route values when redirecting to another action or controller.

Example

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("About", "Other", new { id = 123 });
    }
}

public class OtherController : Controller
{
    public ActionResult About(int id)
    {
        // Retrieve the route value
        return View(id);
    }
}

5. Using Dependency Injection or Shared Service

Use a shared service (e.g., singleton or scoped service) to store and retrieve data between controllers.

Example

public class DataService
{
    private string _sharedData;
    public void SetData(string data) => _sharedData = data;
    public string GetData() => _sharedData;
}

public class HomeController : Controller
{
    private readonly DataService _dataService;

    public HomeController(DataService dataService)
    {
        _dataService = dataService;
    }

    public ActionResult Index()
    {
        _dataService.SetData("Data shared via DI");
        return RedirectToAction("About", "Other");
    }
}

public class OtherController : Controller
{
    private readonly DataService _dataService;

    public OtherController(DataService dataService)
    {
        _dataService = dataService;
    }

    public ActionResult About()
    {
        var sharedData = _dataService.GetData(); // Retrieve the shared data
        return View(sharedData);
    }
}

Next