Social Media And Global Computing View and Session Data
ViewData, ViewBag, and TempData In ASP.NET MVC there are three objects available to pass data to views Design ViewData - For passing data from a controller to a view ViewBag TempData For passing between views We will address using TempData later in the class
ViewData When you use ViewData data you get Access to a ViewData objects only last during the current request Access to a ViewData object can require typecasting for getting data Controller Ex: ViewData["Message"] = "Some Value"; In View Ex: <h2>@ViewData["Message"] </h2>
ViewBag When you use ViewBag data you get Access to a ViewBag objects only last during the current request Access to a ViewBag object does not require typecasting for getting data Controller Ex: ViewBag.Message = "Some Value"; In View Ex: <h2>@ViewBag.Message </h2>
Session When you use Session data you get Access to a Session objects last during the life of the user’s session (about 20 minutes when not authenticated). Access to a Session object does require typecasting for getting data Session is mainly for method-to-method message passing Controller Ex: Session["Message"] = "Some Value"; In View Ex: <h2>@Session["Message"] </h2>
Raw Data If you put HTML in a field, you need to use HTML.Raw() to treat the field to maintain the HTML formatting: <h2>@HTML.Raw(ViewBag.Message )</h2>