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();



Message:- @ViewBag.Message