Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewbag or viewdata. #803

Closed
Anderman opened this issue Dec 15, 2015 · 7 comments
Closed

viewbag or viewdata. #803

Anderman opened this issue Dec 15, 2015 · 7 comments

Comments

@Anderman
Copy link
Contributor

I though viewdata was the replacement for viewbag because it was faster. But i can't find it anymore on github.

In some samples viewbag is stil used. Should this be replace by viewdata?

see http://docs.asp.net/projects/mvc/en/latest/views/view-components.html#add-invokeasync-to-the-priority-view-component

@shirhatti
Copy link
Contributor

@Eilon Is there guidance from the product team on when to use ViewBag vs ViewData or is the choice purely stylistic?

@Anderman
Copy link
Contributor Author

Anderman commented Jan 4, 2016

Aha, I found the issue where I was looking for , but it is only about performance and not preferred style
Since ViewData (Dictionary) look-ups far out-perform ViewBag (dynamic) invocations, the last is probably the best choice.

@Anderman
Copy link
Contributor Author

Anderman commented Jan 4, 2016

Also the templates changed from viewbag to viewdata in beta7 or beta8

@Eilon
Copy link
Member

Eilon commented Jan 14, 2016

@Anderman I think the conclusion in that quote is inadvertently reversed.

It says:

Since ViewData (Dictionary) look-ups far out-perform ViewBag (dynamic) invocations, the last is probably the best choice.

But I'm quite certain it meant to say:

Since ViewData (Dictionary) look-ups far out-perform ViewBag (dynamic) invocations, the FORMER is probably the best choice.

However, the product team has no particular guidance on this. Both approaches have their pros and cons. Which one looks better is clearly a matter of taste. Neither has any particular advantage over the other when it comes to readability or technical merits. The dictionary lookup is most likely a bit faster, but whether that is of any significance depends on the app, so I would be weary of using that as an initial bit of guidance.

What I would recommend above both of these options is to use a strongly-typed view model, which has numerous technical, performance, readability, refactoring, and other advantages. It does has one disadvantage in that it's "harder to do" in terms of setup and maintenance. When I write apps I often use a mix of both.

@shirhatti
Copy link
Contributor

Closing this issue.
Since ViewBag and ViewData are both okay to use, we will not update the examples.

@Rick-Anderson
Copy link
Contributor

@shirhatti @rachelappel can we find a place to add Eilon's awesome info? @rachelappel can you fit that in your model binding doc?

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

No branches or pull requests

4 participants