Why not to use ViewData, ViewBag or TempData !

There are various ways of passing data from Controller to View or vice versa. For example when passing some values to View we can use ViewData Or TempData Or ViewBag. We set the appropriate values for keys in one of these & we can access it on view where ever we need it. This approach is used very extensively in some books as well as many of the blog posts. Though it is very easy & handy to set the values in ViewData, it has some pitfalls.

Before, we will see how we can pass the values from Controller to ViewData. Consider following simple example which contains a drop down list & a date time which we need to display on View. For that purpose controller would look something like this

[HandleError]

public class HomeController : Controller

{

private ICountryRepository _countryRepository { get; set; }

public HomeController(ICountryRepository CountryRepository)

{

_countryRepository = CountryRepository;

}

public ActionResult Index()

{

ViewData[“Country”] = _countryRepository.GetCountryList();

ViewData[“CurrentDate”] = DateTime.Now;

return View();

}

}

Here in Home controller we are setting the values for 2 entities one is Country & other is CurrentDate. Country is a drop down list whereas CurrentDate will display the current date time. The GetCountryList() function here returns IEnumerable.

The view for above will be like this

<%@ Page Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”TitleContent” runat=”server”>

Home Page

</asp:Content>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>

Country : <%: Html.DropDownList(“Country”, ViewData[“Country”] as SelectList) %>

Current DateTime : <%: ViewData[“CurrentDate”]%>

</asp:Content>

Though this looks pretty simple, however we are doing a lot of hardcoded string passing which is not at all good. The main pain with passing hardcoded strings is they can never be cause on compile time. Passing hardcoded string is not a big problem, we can solve it by using string Constants. But ideally, string hardcode should be avoided in order to avoid future mess which may occur during the maintenance phase of the project. Also we need to consider situations where we may need to show more than 10 items on the view, are we going to keep all these items to ViewData ? Also, see how we need to cast ViewData[“Country”]  to SelectList in order to pass it to the DropDownList helper function. While using ViewData all across the application, we need to take this pain.

Though this post was all about not using ViewData, ViewBag or TempData, there are situations where we need to use these as we cannot use View Models there. We will discuss these things in later posts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s