Why is ViewData used when we have ViewBag? - asp.net-mvc

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?

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.

Related

Asp.net MVC way to pass data to _layout.cshtml?

I keep reading that the MVC way to pass data from a controller to the view is done via a ViewModel, but what about passing data to _Layout.cshtml, like page title, meta description, article author, etc...
What's the MVC way to pass this kind of data? Should I just use ViewBag for them?
You have few ways:
ViewBag
ViewData
View Components
Injections (example below)
ViewBag and ViewData are quite easy to use, however not always convenient.
There is one big plus - you could set/read them in one place of view and read in another - for example, you could set them in your main view and read/display them in _lauout.cshtml.
View Components are the most interesting new feature in MVC Core (in my opinion) which allows you to create UI widgets.
There is a little bit more coding for ViewComponent (you need to create controller and view), but it's flexible feature (I like it) and easy to call in a place where you need it, just
#await Component.InvokeAsync("NameOfCOmponent").
Injections not my favorite, but sometime usfull - for example if you want display user name, you could just put the following code directly into your layout/view file:
#using System.Security.Claims
#using Microsoft.AspNetCore.Identity
#inject UserManager<ApplicationUser> userManager
#{
var userInfo = ((await userManager?.GetUserAsync(User))?.xxx);
// where 'xxx' is any property of ApplicationUser model
}
then you can use #userInfo in the same view to display that info.
More information:
Views Overview
Passing Data to Views
View Components

Which way is better to populate the Dropdownlist in MVC?

I Have a List like
IEnumerable<SelectListItem> Users;
I can populate the Users Items in Dropdownlist by 3 way
1-Use ViewModel
Public class myViewModel
{
IEnumerable<SelectListItem> UserList;
}
and fill it like
viewmodel.UserList=GetUsers();
2-Use from ViewBag
ViewBag.UserList=GetUsers();
3-Use from ViewData
ViewData["Users"]=GetUsers();
What is Difference between my ways and which one is better
There's a fourth way, which I think is the best way to go.
Since you only have one object (of type IEnumerable<SelectListItem>) you could just pass it to your view as the model (no need for an intermediate ViewModel).
In terms of the possibilities, there's no real difference. The difference is that your first method and the method I just described are strongly typed, meaning you get Intellisense and compile-time validation whereas your second and third method are weakly typed and you get no intellisense and no compile-time validation.
In your case, better to use ViewModel because it's clean MVC and you get strongly-type benefits.
ViewBag and ViewData are better, for example, if you have a lot of partial views in your view, or difficult layout which need passed data. But as I understood, you need only to show dropdownlist, so use ViewModel.

View text population using ViewBag

I have a partial view with some text that can be modified using the ViewBag
#(ViewBag.FooText ?? "foo")
I populate ViewBag.FooText in the parent view from a resource file:
#
{
ViewBag.FooText = MyResources.Common.FooText
}
My question is whether this is the best place to populate this property (and all other text resources) or would the related controller, or somewhere else, be more appropriate?
I would suggest not using ViewBag if you can help it. It is better to use a strongly-typed viewmodel object, bind the resources to that object (in the controller or in the object itself), and then push the viewmodel to the partial view.
ViewBag isn't the best place to "populate" any property or data.
It will be much better if you store it in the Model. Model View Controller...
Read this answer of #Darin Dimitrov, an MVC master over here...

MVC Razor ViewBag context?

I would like to set values for the ViewBag in a custom class I have created to handle my viewstate (for want of a non WebForms word!). I am aware that ViewData is a property of Controller base. Is there a way I can access the ViewBag from my class? .. another thought, as I want to do this in a class which is being auto-generated by the model binderr, is it possible to accomplish this by implementing a custom binder?
Regards.

ASP.NET MVC: Controller ViewData & ViewPage ViewData

I seem to be unable to find the "link" between a controller's ViewData collection and a ViewPage's ViewData collection. Can anyone point me to where in the MVC framework a controlle's ViewData collection is transferred to a ViewPage's ViewData collection?
I have spent quite a lot of time using Reflector to try and work this one out but I'm obviously not looking in the right place.
The Controller.View method transfers the ViewData into the ViewResult.
ViewResult.ExecuteResult transfers this into its ViewContext.
In WebFormView, the private RenderViewPage method transfers the ViewData from the context argument to the view itself. Other view in genes may work differently.

Resources