RKS
RKS

Reputation: 1410

Why is ViewData used when we have ViewBag?

ViewData & ViewBag serve the same purpose of transferring data from Controller to View or between Views. The difference between them is the underlying implementation and the way we need to handle it as a result.(casting in case of ViewData etc.)

So, can there be any scenario where ViewData is preferred over ViewBag?

Upvotes: 2

Views: 811

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

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.

Being a wrapper it holds no data itself - it's just a shortcut for accessing ViewData. You have no reason to use the ViewData directly nowdays.

Upvotes: 2

Related Questions