TempData In ASP.Net MVC

Passing Data From A Controller To A View In ASP.Net MVC

In an ASP.NET MVC application, a controller typically performs the business logic of the application and needs to return the result to the user through a view.

You can use the following objects to pass data between controller and view:

  1. ViewData
  2. ViewBag
  3. TempData

What Is TempData ?

  • TempData is a Dictionary object derived from the TempDataDictionary class.
  • TempData stores data as key-value pairs.
  • Example: TempData[“Var”] = “Adil”
  • TempData value must be type cast before use. Check for null values to avoid runtime error.
  • TempData allows passing data from the current request to the subsequent request during request redirection.

The general syntax of TempData is as follows:

TempData[<Key>] = <Value>;

where,

  • Key: Is a String value to identify the object present in TempData.
  • Value: Is the object present in TempData.
  • TempData in ASP.NET MVC can be used to store temporary data which can be used in the subsequent request. TempData will be cleared out after the completion of a subsequent request.
  • Call TempData.Keep() method to keep all the values of TempData in a third request.

Source Code Of HomeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TempDataDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewData["Var1"] = "Message from View Data";
            ViewBag.Var2 = "Message from View Bag";
            TempData["Var3"] = "Message from Temp Data";

            //string[] games = {"Cricket","Hockey","Football","Baseball" };

            //TempData["GamesArray"] = games;

            return RedirectToAction("About");
            //return View();
        }

        public ActionResult About()
        {    
            if(TempData["Var3"] != null)
            {
                TempData["Var3"].ToString();
            }

            TempData.Keep();
            return View();
        }
        public ActionResult Contact()
        {
            if (TempData["Var3"] != null)
            {
                TempData["Var3"].ToString();
            }
            return View();
        }
        
    }
}

Source Code Of Index.cshtml (View)


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

Source Code Of About.cshtml (View)


@{
    ViewBag.Title = "About";
}

<h2>About</h2>

<h3>ViewData data is = @ViewData["Var1"]</h3>
<h3>ViewBag data is = @ViewBag.Var2</h3>
<h3>TempData data is = @TempData["Var3"]</h3>

@*<ul>
    @{
        foreach (string i in (string[])TempData["GamesArray"])
        {
            <li>@i</li>
        }
    }
</ul>*@

Source Code Of Contact.cshtml (View)


@{
    ViewBag.Title = "Contact";
}

<h2>Contact</h2>
<h3>ViewData data is = @ViewData["Var1"]</h3>
<h3>ViewBag data is = @ViewBag.Var2</h3>
<h3>TempData data is = @TempData["Var3"]</h3>

Click Below Link to Download Notes Of This Blog

https://www.mediafire.com/file/0k2u97aplr2k2iv/TEMP+DATA+IN+MVC.docx/file

Click Below Link to Download Source Code Project Of This Blog

https://www.mediafire.com/file/41ynnczqvwppbyj/TempDataDemo.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *