Introduction
In this article, you will learn what is ViewData
, ViewBag
and TempData
. Our goal in this tip is to enable intellisense for ViewBag
dynamic object in ASP.NET MVC. That sometimes happens in the projects, the programmer, the list to be sent to the View
with ViewBag
.
Background
ViewBag
is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- Basically, it is a wrapper around the
ViewData
and also used to pass data from controller to corresponding view.
ViewBag
is a property of ControllerBase
class.
- Its life also lies only during the current request.
- If redirection occurs, then its value becomes
null
.
- It doesn't require typecasting for getting data.
I create persons
class in Models folder with the following properties:
using System.Web;
namespace Intellisense.Models
{
public class Persons
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FatherName { get; set; }
public int Age { get; set; }
public int Mobile { get; set; }
public string Address { get; set; }
}
}
?
Now, turn to build Index
action in HomeController
and initialize ViewBag
with list of persons to send ViewBag.Persons
to view:
using System.Collections.Generic;
using System.Web.Mvc;
using Intellisense.Models;
namespace Intellisense.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var listOfPerson = new List<Persons>
{
new Persons() {Id = 1, FirstName = "Jone", LastName = "liy",
FatherName = "Sobin", Age = 36, Mobile = +982015222, Address = "..."},
new Persons() {Id = 2, FirstName = "kety", LastName = "sory",
FatherName = "petter", Age = 19, Mobile = +962222155, Address = "..."},
};
ViewBag.Persons = listOfPerson;
return View();
}
}
}
?
In the View can be used two ways in the list of existing submissions ViewBag.Persons
calling:
- Use dot ( . )
- Casting
Dot ( . )
In the following code, I use dot for show persons in ViewBag.Persons
, but the disadvantages of using this method is that the name typos properties after the dot (.), also it is not active for intellisense.
@{
ViewBag.Title = "ViewBag";
}
<table>
<thead>
<tr>
<th>
Id
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Father Name
</th>
<th>
Age
</th>
<th>
Mobile
</th>
<th>
Address
</th>
</tr>
</thead>
<tbody>
@{foreach (var item in ViewBag.Persons)
{
<tr>
<td>
@item.Id
</td>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.FatherName
</td>
<td>
@item.Age
</td>
<td>
@item.Mobile
</td>
<td>
@item.Address
</td>
</tr>
}
}
</tbody>
</table>
Cast
Using the keyword do is accepted as practice casting below, two methods for casting. In this case, the active intellisense as well:
@using Intellisense.Models
@{
ViewBag.Title = "ViewBag";
// The first method
// It is recommended that the first method should be used
// var listOfPerson = ViewBag.Persons as IEnumerable<Persons>;
// The second method
// var listOfPerson = (IEnumerable<Persons>)ViewBag.Persons;
var listOfPerson = ViewBag.Persons as IEnumerable<Persons>;
}
<table>
<thead>
<tr>
<th>
Id
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Father Name
</th>
<th>
Age
</th>
<th>
Mobile
</th>
<th>
Address
</th>
</tr>
</thead>
<tbody>
@{foreach (var item in listOfPerson)
{
<tr>
<td>
@item.Id
</td>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.FatherName
</td>
<td>
@item.Age
</td>
<td>
@item.Mobile
</td>
<td>
@item.Address
</td>
</tr>
}
}
</tbody>
</table>
Reference
I hope this will help you.