Using ViewBag in ASP.NET CORE 3.0

ViewBag is Use to pass data from controller to View only and its value cannot be retained. ViewBag is a dynamic type it does not require typecasting for the complex type of object.

If you want to send data from controller to view and must not have an issue with types then you can use ViewBag.

We are going add Controller with Name Demo3 with Index action method inside that we are going to declare ViewBag with a string value.

using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class Demo3Controller : Controller
    {
        public IActionResult Index()
        {
            ViewBag.Message = "Hi Message";
            return View();
        }
    }
}

View

<h1>Index</h1>

Message:- @ViewBag.Message

By Saineshwar Bageri

I am Microsoft MVP | C# Corner MVP | Code Project MVP | FULL STACK .NET Developer and working on .Net Web Technology (Asp.net, Asp.net Core,.Net Core, C#, Sqlserver, MVC, Windows, Console Application, javascript, jquery, json, ORM Dapper) and also a freelance developer.