Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Helpers do not create copies of ViewDataDictionary when contextualized #954

Closed
pranavkm opened this issue Aug 8, 2014 · 2 comments
Closed

Comments

@pranavkm
Copy link
Contributor

pranavkm commented Aug 8, 2014

Started this as a way to ensure we're aware of a behavior change between Mvc 5 and Mvc 6. In Mvc 5, initializing a helper (HtmlHelper<T>) would cause it to shallow copy the passed in ViewDataDictionary. This meant the Html.ViewData instance is different from the ViewData instance on the WebViewPage. Consequently, mutating Html.ViewData would leave the ViewData unaffected - it had the same behavior as a partial. This resulted in behavior like this:

@{
    ViewData["foo"] = "Hello world";
    Html.ViewData["foo"] = "something else";
}   
<h1>@(ViewData == Html.ViewData)</h1>
<h2>Html: @Html.ViewData["foo"]</h2>
<h2>ViewData: @ViewData["foo"]</h2>

False
Html: something else
ViewData: Hello world

In Mvc 6, helpers do not create a copy of the ViewData and reference it directly from the passed in ViewContext. This produces the result

True
Html: something else
ViewData: something else

IMO, the behavior in Mvc 6 seems like the correct behavior, but I may be missing some scenarios that are affected by this change.

cc @Eilon \ @dougbu

@Eilon
Copy link
Member

Eilon commented Aug 12, 2014

I bet there was some super smart reason for how it was in MVC 1 - 5... not...

I can see arguments for having it either way, but I have to admit that whenever I hear "shallow copy" I cringe a little bit because it often falls into the category of "why bother." It's sort of a half-a-cat scenario (not always, but I think in this case it is).

I think I'm supportive of going with the new behavior and seeing how it plays out...

@pranavkm
Copy link
Contributor Author

We can revive this later on if we find this to be an issue. Closing this for now since no work needs to be done.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants