Follow us

Header Ads

TempData in Asp.net Core

 TempData in Asp.net Core

Package

"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5"

Controllers[Folder]

HomeController.cs

using Microsoft.AspNetCore.Mvc;

namespace TempData.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewData["var1"] = "View Data";
            ViewBag.var2 = "View Bag";
            TempData["var3"] = "Temp Data";
            string[] arr = { "Adil", "Zain", "Ali" };
            TempData["var4"] = arr;
            return View();
        }
        public IActionResult About()
        {
            TempData.Keep("var3");
            return View();
        }
        public IActionResult Contact()
        {
            return View();
        }
    }
}


Views[Folder]

About.cshtml


@{
    ViewData["Title"] = "About";
}

<h1>About</h1>
@ViewData["var1"] 
@ViewBag.var2 
@TempData["var3"] 
@{
    foreach (var item in(string[]) TempData["var4"])
    {
        <h1> @item</h1>
       
    }
}

Contact.cshtml


@{
    ViewData["Title"] = "Contact";
}

<h1>Contact</h1>
@ViewData["var1"] 
@ViewBag.var2 
@TempData["var3"] 

Index.cshtml


@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>
@*@ViewData["var1"] 
@ViewBag.var2 
@TempData["var3"] *@


Post a Comment

0 Comments