How we use tempdata in controller from another controller in asp.net mvc?


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

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


Asked by:- vikas_jk
1
: 4127 At:- 11/7/2017 10:12:44 AM
asp.net 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 asp.net 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

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






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

Full Name *

Email *




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