Here we will learn viewbag in asp.net mvc, tempdata, and viewdata in asp.net mvc with example and how to use viewbag, tempdata, and viewdata in asp.net mvc to send or pass data from controller to view with example.
We have three options to pass data from controller to view in asp.net mvc application. Those are
TempData in MVC is used to pass data from Controller to Controller or Controller to view, and it is used to pass data from the current request to the next request.
TempData has a short life, and it required typecasting. Following is the code snippet of TempData in MVC.
// Summary:
// Gets or sets the dictionary for temporary data.
//
// Returns:
// The dictionary for temporary data.
public TempDataDictionary TempData { get; set; }TempData is a dictionary object that is derived from the TempDataDictionary class. We will see how we can assign value to TempData in Controller with a simple example for that create one new controller in the application and write code like as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
TempData["test"] = "hi Tempdata";
return View();
}
}
}
We will create one view to show data for that right click on controller à select add view and give a name for that view (Index.cshtml). Open view file and write the following code to show data on the website.
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@TempData["test"]
When we run the above code, we will get output like as shown below.
ViewData is used to pass data from controller to view, and it contains a null value when redirection occurs. ViewData is derived from ViewDataDictionary that is dictionary type, and ViewData required typecasting. Following is the code snippet of ViewData in MVC.
// Summary:
// Gets or sets the dictionary for view data.
//
// Returns:
// The dictionary for the view data.
public ViewDataDictionary ViewData { get; set; }
ViewData is a dictionary object that is derived from the ViewDataDictionary class. We will see how we can assign value to ViewData in Controller with a simple example for that create one new controller in the application and write code like as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
ViewData["test"] = "hi Viewdata";
return View();
}
}
}
Now we will create one view to show data for that right click on controller à select add view give a name for that view (Index.cshtml). Open view file and write the following code to show data on the website.
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewData["test"]
ViewBag is used to pass data from controller to view, and it does not require typecasting. ViewBag contains a null value when redirection occurs, and the following is the code snippet of viewbag in MVC.
// Summary:
// Gets the dynamic view data dictionary.
//
// Returns:
// The dynamic view data dictionary.
[Dynamic]
public dynamic ViewBag { get; }
Generally, ViewBag uses a dynamic type that was introduced in visual C# 4.0. We will see how we can assign value to ViewBag in Controller with a simple example for that create one new controller in the application and write code like as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
ViewBag.Test = "hi ViewBag";
return View();
}
}
}
Now we will create one view to show data for that right click on controller à select add view give a name for that view (Index.cshtml). Open view file and write the following code to show data on the website.
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewBag.Test
By using the above methods of TempData, ViewData, and ViewBag, we can pass data from controller to view in asp.net MVC application.