Introduction:

In this article we will explain what is  TempData in asp.net  mvc or how to use TempData in asp.net  or how to pass data one controller to another controller in Asp.net MVC with example.

Description:

ASP.NET MVC TempData is dictionary derived from TempData Dictionary class. TempData is used for passing data from controller to another controller or one action to other action as well as redirects. It internally uses session variables. It requires typecasting for complex data type and check for null values to avoid error.

Following example, when we execute my application first call index action and redirect to UserList action.


Example: Set TempData in Action method


using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {              public ActionResult Index()
        {
            IList<User> UserList = new List<User>() {
                    new User(){ id=1, Name="Mack", Age =21 },
                    new User(){ id=2, Name="Mark", Age =25 },
                    new User(){ id=3, Name="Johan", Age =20 },
                    new User(){ id=4, Name="Brain", Age =31 },
                    new User(){ id=5, Name="Myank", Age =19 }
                };
            TempData["users"] =UserList;
            TempData["count"] =UserList.Count;
            return RedirectToAction("UserList");
        }
 
        public ActionResult UserList()
        {
            UserModel obj = new UserModel();
            obj.Users = (List<User>)TempData["users"];
            obj.Count =(int) TempData["count"] ;
            return View(obj);
        }
    }     }


Example: Acess TempData in a View

 

UserList.cshtml


@using MvcApplication1.Models
@model MvcApplication1.Models.UserModel
<table border="0">
    <tr>
        <td colspan="3"><h2>Mvc Application Use Model List</h2></td>
    </tr>
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Age</th>
    </tr>
    @foreach (var item in Model.Users)
    {
        <tr>
            <td>@item.id</td>
            <td>@item.Name</td>
            <td>@item.Age</td>
        </tr>
    }
 
   
    <tr>
        <td colspan="2">Total Users</td>
        <td>@Model.Count</td>
    </tr>
</table>
 
 
<table border="0">
    <tr>
        <td colspan="3"><h2>Mvc Application Use TempData</h2></td>
    </tr>
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Age</th>
    </tr>
   
 
    @foreach (var item in TempData["users"] as List<User>)
    {
        <tr>
            <td>@item.id</td>
            <td>@item.Name</td>
            <td>@item.Age</td>
        </tr>
    }
    <tr>
        <td colspan="2">Total Users</td>
        <td> @TempData["count"]</td>
    </tr>
</table>
 
<style>
    table {
        width: 500px;
        margin: 0 auto;
    }
 
    table, th, td {
        border: 1px solid grey;
        border-collapse: collapse;
        padding: 5px;
    }
 
        table tr:nth-child(odd) {
            background-color: #f3d3d3;
        }
 
        table tr:nth-child(even) {
            background-color: #ffffff;
        }
</style>


OutPut:


TempData inTempData In Mvc


i hope it will help to you.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like