Gabe
Gabe

Reputation: 50523

ViewDataDictionary differences

What is the difference of ViewDataDictionary properties for:

Upvotes: 0

Views: 263

Answers (1)

Tim
Tim

Reputation: 15237

Initially, there's no difference.

HtmlHelper<T>.ViewData is set by the constructor (well, more accurately, the private field _viewData is set by the constructor, but that's what the public property ViewData returns) from the IViewDataContainer that is passed into the constructor.

HtmlHelper<T> derives from HtmlHelper. HtmlHelper has the property ViewDataContainer. That is set in the constructor from the IViewDataContainer that is passed into the constructor.

Essentially, HtmlHelper<T>.ViewData is just a "shortcut" to get to HtmlHelper<T>.ViewDataContainer.ViewData

There is one caveat, though. Notice I said initially there's no difference.

The HtmlHelper<T> constructor sets _viewData to a NEW instance of a ViewDataDictionary, passing in the IViewDataContainer's ViewData property.

What that means is that HtmlHelper<T>.ViewData is initially a copy of HtmlHelper.ViewDataContainer.ViewData, but it is not the same instance. The data that the two contain will be the same initially. But if you make changes to one, you're not making changes to the other.

Upvotes: 2

Related Questions