Following show the step by step example how to use TempData,ViewData,ViewBag :
Step 1:
Create a simple application in MVC then you have to display the structure show in below.
A : Create a one models with name 'Employee.cs shown below code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
WebApplication2.Models
{
public
class
Employee
{
public
int
EmployeeID { get;
set;
}
public
string
Name { get;
set;
}
public
string
Department { get;
set;
}
}
}
B : Create a one controllers TestController.cs shown below code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
WebApplication2.Models;
namespace
WebApplication2.Controllers
{
public
class
TestController
: Controller
{
//
GET: Test
public
ActionResult
Index()
{
List<Employee>
employeeList = new
List<Employee>();
Employee
employee = new
Employee();
employeeList.Add(new
Employee
{ EmployeeID = 1, Name = "Sandeep",
Department = "InformationTech"
});
employeeList.Add(new
Employee
{ EmployeeID = 2, Name = "Pranali",
Department = "HRDepartment"
});
employeeList.Add(new
Employee
{ EmployeeID = 3, Name = "Smith",
Department = "Operation"
});
//
ViewBag.EmployeeList = employeeList;
ViewData["EmployeeList"]
= employeeList;
ViewBag.EmployeeNameASP
= "Pranali
";
ViewData["EmployeeNameC#"]
= "Sandeep";
TempData["EmployeNameMVC"]
= "Jamkar";
//
return View(employeeList);
TempData.Keep();
return
View();
}
public
ActionResult
Mypage()
{
TempData.Keep();//before
add keep method here you have to load the page the temp data values
not preserved but after put the keep method in secod action or page
it is
return
View();
}
}
}
C : After create the controllers add the view of the ActionResult in TestController then shows the structure below.
Views --->Test Folder---->Index.cshtml then write the below code.
@{
ViewBag.Title
= "Index";
}
<h2>Index</h2>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Department</th>
</tr>
<tbody>
@*@if(Model
!=null)
{*@
@*//foreach(var
item in Model) disply list using model*@
@*@foreach
(var item in ViewBag.EmployeeList) disply list using viewbag*@
@foreach(var
item in
ViewData["EmployeeList"]
as
List<WebApplication2.Models.Employee>)
{
<tr>
<td>@item.EmployeeID</td>
<td>@item.Name</td>
<td>@item.Department</td>
</tr>
}
@*}*@
</tbody>
</table>
<div
class="container">
<ul
class="list-group">
<li
class="list-group-item">
your
viewBag values is <b>@ViewBag.EmployeeNameASP</b>
</li>
<li
class="list-group-item">
your
viewData values is <b>@ViewData["EmployeeNameC#"]</b>
</li>
<li
class="list-group-item">
your
tempBag values is <b>@TempData["EmployeNameMVC"]</b>
</li>
<li
class="list-group-item">
<a
href="/Test/Mypage">Go
To My PAge</a>
</li>
</ul>
</div>
We are write above code of the all required implementation like TempData,ViewData,ViewBag and Keep Method in the TempData.
Step 2: In this step we implement one by one concept and UN comment required code comment remaining code then RUN the Application.
A. Model Data
TestController.cs---->ActionResult Write below code.
public
ActionResult
Index()
{
List<Employee>
employeeList = new
List<Employee>();
Employee
employee = new
Employee();
employeeList.Add(new
Employee
{ EmployeeID = 1, Name = "Sandeep",
Department = "InformationTech"
});
employeeList.Add(new
Employee
{ EmployeeID = 2, Name = "Pranali",
Department = "HRDepartment"
});
employeeList.Add(new
Employee
{ EmployeeID = 3, Name = "Smith",
Department = "Operation"
});
return
View(employeeList);
}
And Some changes in Index.cshtml write below code
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Department</th>
</tr>
<tbody>
@if(Model
!=null)
{
foreach
(var
item in
Model)
{
<tr>
<td>@item.EmployeeID</td>
<td>@item.Name</td>
<td>@item.Department</td>
</tr>
}
}
</tbody>
</table>
Then run the application shows below screen.
http://localhost:62418/Test/Index