January 30, 2024 06:34 by
Peter
Using TempData, you may transfer temporary data between controllers in ASP.NET Core MVC. A dictionary called TempData can be used to transfer information between controllers both during the current request and the one that comes after. Here's how you can make this happen.
Configure the First Controller's TempData
Assign the data you wish to pass to the TempData in your first controller action.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Student student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
// Transfer the Complete Student Object to the Teacher
TempData["student"] = student;
return RedirectToAction("Index","Teacher");
}
Complete Student Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using DataTransferBetweenControllersinASPNETCoreMVC.DatbaseContext;
using DataTransferBetweenControllersinASPNETCoreMVC.Models;
namespace DataTransferBetweenControllersinASPNETCoreMVC.Controllers
{
public class StudentsController : Controller
{
private readonly AppDbContext _context;
public StudentsController(AppDbContext context)
{
_context = context;
}
// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context.Students.ToListAsync());
}
// GET: Students/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.FirstOrDefaultAsync(m => m.Id == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// GET: Students/Create
public IActionResult Create()
{
return View();
}
// POST: Students/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Student student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
// Transfer the Complete Student Object to Teacher
TempData["student"] = student;
return RedirectToAction("Index","Teacher");
}
// GET: Students/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students.FindAsync(id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Students/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,RollNo,Section,Program")] Student student)
{
if (id != student.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(student);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(student.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(student);
}
// GET: Students/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.FirstOrDefaultAsync(m => m.Id == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var student = await _context.Students.FindAsync(id);
if (student != null)
{
_context.Students.Remove(student);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool StudentExists(int id)
{
return _context.Students.Any(e => e.Id == id);
}
}
}
Retrieve TempData in the Second Controller
In your second controller action, retrieve the TempData.
// GET: Teachers
public async Task<IActionResult> Index()
{
// Recieve the Student Object data and then keep the data
// Keep TempData for the next request
var studentData = TempData["student"];
TempData.Keep();
return View(await _context.Teachers.ToListAsync());
}
Complete Teacher Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using DataTransferBetweenControllersinASPNETCoreMVC.DatbaseContext;
using DataTransferBetweenControllersinASPNETCoreMVC.Models;
namespace DataTransferBetweenControllersinASPNETCoreMVC.Controllers
{
public class TeachersController : Controller
{
private readonly AppDbContext _context;
public TeachersController(AppDbContext context)
{
_context = context;
}
// GET: Teachers
public async Task<IActionResult> Index()
{
// Recieve the Student Object data and then keep the data
var studentData = TempData["student"];
TempData.Keep();
return View(await _context.Teachers.ToListAsync());
}
// GET: Teachers/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var teacher = await _context.Teachers
.FirstOrDefaultAsync(m => m.Id == id);
if (teacher == null)
{
return NotFound();
}
return View(teacher);
}
// GET: Teachers/Create
public IActionResult Create()
{
return View();
}
// POST: Teachers/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Course,Department")] Teacher teacher)
{
if (ModelState.IsValid)
{
_context.Add(teacher);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(teacher);
}
// GET: Teachers/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var teacher = await _context.Teachers.FindAsync(id);
if (teacher == null)
{
return NotFound();
}
return View(teacher);
}
// POST: Teachers/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Course,Department")] Teacher teacher)
{
if (id != teacher.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(teacher);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TeacherExists(teacher.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(teacher);
}
// GET: Teachers/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var teacher = await _context.Teachers
.FirstOrDefaultAsync(m => m.Id == id);
if (teacher == null)
{
return NotFound();
}
return View(teacher);
}
// POST: Teachers/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var teacher = await _context.Teachers.FindAsync(id);
if (teacher != null)
{
_context.Teachers.Remove(teacher);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool TeacherExists(int id)
{
return _context.Teachers.Any(e => e.Id == id);
}
}
}
Keeping TempData for Subsequent Requests
By default, TempData is meant for a single subsequent request. If you want to persist TempData for more than one subsequent request, you can use the Keep method.
// GET: Teachers
public async Task<IActionResult> Index()
{
// Recieve the Student Object data and then keep the data
var studentData = TempData["student"];
TempData.Keep();
return View(await _context.Teachers.ToListAsync());
}
Conclusion
TempData is an efficient way to pass temporary data across controllers in ASP.NET Core MVC. You can save and retrieve information for the current request and the one that follows using TempData. You can enable data sharing between several components of your application by setting TempData in one controller and retrieving it in another.
Keep in mind that TempData is only meant to be read once before being marked for deletion. If you need to store the data for more than one request, use the Keep method.
A handy way to share transient data between controllers when a user interacts with your ASP.NET Core MVC application is via TempData.