How we use tempdata in controller from another controller in mvc?

How we use temp data (or pass values) in one controller from another controller in mvc? 

Originally asked by user_2107228942 on question "how to upload image in C# MVC?"

Asked by:- vikas_jk
: 221 At:- 11/7/2017 10:12:44 AM mvc C# temp data passing values from one controller to another

1 Answers
profileImage Answered by:- vikas_jk

You can pass data from one controller to another in MVC, Suppose you need to pass value from Index ActionMethod of HomeControlller to Index ActionMethod of Home2Controller

HomeController -Index Method Code will be

public ActionResult Index()
    Person data = new Customer()
        Id = 1,
        Name = "Abcd"       
    TempData["mydata"] = data; // save your values in Temp data
    return RedirectToAction("Index", "Home2"); //Call Index ActionMethod of Home2 controller

And to get values in Home2Controller using Tempdata, your code will be

public ActionResult Index()
    Person data = TempData["mydata"] as Person ; //Get data as Person type
    return View(data);

here Person is Model

At:- 11/7/2017 10:20:32 AM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use