Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Enabling Intellisense for ViewBag in ASP.NET MVC

0.00/5 (No votes)
7 Aug 2015 2  
In this tip, you will learn how to enable intellisense for ViewBag in ASP.NET MVC

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

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  2. Basically, it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
  3. ViewBag is a property of ControllerBase class.
  4. Its life also lies only during the current request.
  5. If redirection occurs, then its value becomes null.
  6. 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
    {
        // GET: Home
        public ActionResult Index()
        {
            // List of person
            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 = "..."},
            };
            // Set ViewBag.Persons data from listOfPerson
            ViewBag.Persons = listOfPerson;
            // Initialize and send ViewBag.Persons to view
            return View();
        }
    }
}
?

In the View can be used two ways in the list of existing submissions ViewBag.Persons calling:

  1. Use dot ( . )
  2. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here