DEV Community

Elanat Framework
Elanat Framework

Posted on

ViewData in CodeBehind: Data Transfer Across MVC Components

The ViewData feature in the CodeBehind framework, developed by Elanat, is a simple and practical mechanism for transferring data between different components of the MVC architecture.


What is ViewData?

ViewData is a data list of type NameValue (Key-Value).
This structure allows you to store data with a specific key and retrieve it in other parts of the same request.

ViewData is used to transfer data between MVC components and supports both reading and writing in the following sections:

  • Controller
  • Model
  • View
  • Layout

ViewData Lifetime

An important point to consider is that the data stored in ViewData is valid only during the current HTTP request.

This means that once the request is completed and the response is sent to the client, the ViewData values are cleared and will not be available in subsequent requests.


Comparison with ASP.NET

The overall behavior of ViewData in CodeBehind is very similar to ViewData in:

  • ASP.NET MVC
  • ASP.NET Razor Pages

If you have previously worked with these technologies, using ViewData in CodeBehind will feel familiar and intuitive.


Writing Priority in ViewData

The writing priority order in ViewData is as follows:

  1. Controller
  2. Model
  3. View

This reflects the execution flow of the MVC pattern.

If a new ViewData value is created inside the View, it cannot be read in the Controller, because the Controller executes before the View.


Example: Using ViewData in a View

In the following example, the title value is set inside the View:

@page
@controller CacheThemeController
@layout "/layout.aspx"
@{
  ViewData.Add("title","Cache Theme");
}
Enter fullscreen mode Exit fullscreen mode

Here, the value "Cache Theme" is stored with the key "title".


Accessing ViewData in a Layout

The stored value can be retrieved inside the Layout file:

@page
@islayout
@{
    string WelcomeText = "Welcome to the CodeBehind Framework!";
}
<!DOCTYPE html>
<html>
<head>
    <title>CodeBehind Framework - @ViewData.GetValue("title")</title>
Enter fullscreen mode Exit fullscreen mode

In this example, the title value defined in the View is accessed in the Layout and displayed inside the <title> tag.


Using ViewData in a Controller

You can also assign values inside a Controller:

public void PageLoad(HttpContext context)
{   
    ViewData.Add("name", "Adriano");
}
Enter fullscreen mode Exit fullscreen mode

In this case, the value "Adriano" is stored in ViewData and becomes accessible in the View.


Reading ViewData in a View

@page
@controller ChangeListController

Name: @ViewData.GetValue("name")
Enter fullscreen mode Exit fullscreen mode

Here, the value associated with the specified key is retrieved from ViewData and rendered in the View.


Advantages of ViewData

Using ViewData in CodeBehind offers several advantages:

  • Simple Key-Value structure for lightweight data transfer
  • No need to create additional models for small or temporary data
  • Accessible across Controller, Model, View, and Layout
  • Request-scoped lifetime, ensuring clean and predictable data handling
  • Lightweight and fast, with minimal overhead
  • Familiar pattern for developers coming from ASP.NET MVC or Razor Pages

Conclusion

ViewData in CodeBehind is a lightweight, flexible, and efficient mechanism for transferring data between MVC components within a single request.

It is especially suitable for passing small pieces of data such as page titles, UI settings, temporary messages, or configuration values—without introducing unnecessary complexity into your application architecture.


Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

CodeBehind in NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind

Top comments (0)