How to Send Data Controller to Another Controller View?

Introduction

In this blog how we can send data controller to another controller data. We can send data in many different types, but the easiest way is TempData and TempView.

TempData

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller.

Syntax

TempData["key"] = "value";

Example

TempData["is"] = 'hello';

ViewData

In ASP.NET MVC, "ViewData" is a mechanism used to pass data from a controller to a view. It's a dictionary-like object that allows you to store and retrieve data within the scope of a single HTTP request-response cycle. ViewData is used to communicate between the controller and the view without explicitly using strongly-typed models.

Syntax

ViewData["key"] = "value";

Example

ViewData["Message"] = "Hello from ViewData!";

So It's important to understand the differences between ViewData and TempData.

ViewData is used to pass data from a controller to a view during a single request.

TempData is used to persist data across redirects for one additional request. After that, the data is automatically removed.

While these mechanisms can be helpful in certain scenarios, using strongly-typed models (ViewModels) is generally considered a better practice because they provide type safety, clearer code organization, and easier maintainability in the long run.