Site icon Tutexchange

Using ViewBag in ASP.NET CORE 3.0

Advertisements

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
Exit mobile version