Showing posts with label Keep method in TempData. Show all posts
Showing posts with label Keep method in TempData. Show all posts

Monday, 20 November 2017

Keep method in TempData,ViewData and ViewBag

B. ViewBag Data

Write the below code change in the TestController.


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;


return View();

}

Then some changes required in the Index.cshtml so write below code.

<table>

<tr>

<th>Id</th>

<th>Name</th>

<th>Department</th>

</tr>

<tbody>

@foreach (var item in ViewBag.EmployeeList)

{

<tr>

<td>@item.EmployeeID</td>

<td>@item.Name</td>

<td>@item.Department</td>

</tr>

}


</tbody>



</table>

After run the application you display below screen.
















C. ViewData

Following change in the TestController.

ViewData["EmployeeList"] = employeeList;

Write the following code in the index page but in the ViewData type casting show after add index code shows error.

@foreach (var item in ViewData["EmployeeList"])

{

<tr>

<td>@item.EmployeeID</td>

<td>@item.Name</td>

<td>@item.Department</td>

</tr>

}

Error :foreach statement cannot operate on variables of type 'object' because'object' does not contain a public definition for 'GetEnumerator'
Then write the index code show below.

@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>

}

Then Run The Application Shows result. 

D. TempData

It helps to maintain data when you move from one controller to other controller or from one action to other action TempData.Keep() is used. 

Write the below code in the TestController.cs file.


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" });

ViewData["EmployeeList"] = employeeList;

ViewBag.EmployeeNameASP = "Pranali ";

ViewData["EmployeeNameC#"] = "Sandeep";

TempData["EmployeNameMVC"] = "Jamkar";


return View();

}

We have to create the another page Mypage.cshtml for redirect from one controller to another controller or from one page to another page 

A. Write the below code in the index file.

<tbody>


@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>


B. Write the below code in the Mypage.cshtml



@{

ViewBag.Title = "Mypage";

}



<h2>Mypage</h2>



<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/Index">Go To My PAge</a>

</li>

</ul>

</div>

 Then run the application showing below screen.














Then after redirect to the Mypage it showing the blank null show below result.












So you need TempData Keep() method for manage data in the both page because you have to use Keep() in one page then after redirect and reload the data invisible so write the Keep() method in both page shown 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" });


ViewData["EmployeeList"] = employeeList;

ViewBag.EmployeeNameASP = "Pranali ";

ViewData["EmployeeNameC#"] = "Sandeep";

TempData["EmployeNameMVC"] = "Jamkar";


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();

}

Then Run the application you got result shown below.