Hi Destinykid,
Check this example. Now please take its reference and correct your code.
Model
public class Employee
{
public int EmployeeId { get; set; }
public string Department { get; set; }
public string EmployeeName { get; set; }
}
Controller
Home
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
Employee model = new Employee()
{
EmployeeId = 1,
Department = "Computer Science",
EmployeeName = "Destiny"
};
TempData["EmployeeDetail"] = model;
TempData["Message"] = "This is the Employee Detail fetched with TempData";
return View();
}
public ActionResult Employee()
{
return View();
}
}
Default
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return View();
}
}
View
Home -> Index
@{
Layout = null;
ViewBag.Title = "Employee Details ";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index - Home</title>
</head>
<body>
<h3>Home Controller Index Action</h3>
<div>
@Html.ActionLink("View Employee", "Employee", "Home", null, null)
<br /><br />
@Html.ActionLink("Go to Default", "Index", "Default", null, null)
</div>
</body>
</html>
Home -> Employee
@using Pass_Data_Controller_View_MVC.Models
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Employee - Home</title>
</head>
<body>
<h3>Home Controller Employee Action</h3>
@{
var EmployeeDetail = TempData["EmployeeDetail"] as Employee;
}
@TempData["Message"]<br />
Id: @EmployeeDetail.EmployeeId<br />
Name: @EmployeeDetail.EmployeeName<br />
Department: @EmployeeDetail.Department<br /><br />
@Html.ActionLink("Back to Index", "Index", "Home", null, null)
</body>
</html>
Default -> Index
@using Pass_Data_Controller_View_MVC.Models
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index - Default</title>
</head>
<body>
<h3>Default Controller Index Action</h3>
@{
var EmployeeDetail = TempData["EmployeeDetail"] as Employee;
}
@TempData["Message"]<br />
Id: @EmployeeDetail.EmployeeId<br />
Name: @EmployeeDetail.EmployeeName<br />
Department: @EmployeeDetail.Department<br /><br />
@Html.ActionLink("Back to Index", "Index", "Home", null, null)
</body>
</html>
Screenshot