WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

Can ViewBag and ViewData be used interchangeably?

I was goofing around with MVC 5 and added the following code

@Code
    ViewData("AppName") = "Apricot"
End Code

Later in my Layout page I mistakenly used

Viewbag.AppName

and to my surprise it worked. Is ViewBag simply wrapping ViewData

Upvotes: 0

Views: 179

Answers (1)

Eric
Eric

Reputation: 910

Essentially yes. They are very similar and resolve to the same repository. ViewBag uses C# 4 dynamics and so its properties do not require casting the way Viewdata does. Otherwise you would use them the same. Neither is particularly recommended though.

Upvotes: 2

Related Questions