ASP.NET MVC: Controller ViewData & ViewPage ViewData - asp.net-mvc

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.

Related

Asp.Net MVC View function and ViewData

I have a small confusion in Asp.Net MVC
How rendering works in Asp.net MVC? We invoke View function - > Which will find the view and ask ViewEngine to parse it. Because of ViewEngine final outcome is HTML.
1)Whatever ViewData we create its available inside View. My understanding is ViewData and View function both are part of controller base class which makes ViewData available inside View function. Is it correct?
2)Finally Whats the point with WebViewPage class. ViewData keyword we use inside View(.cshtml) page is coming from the WebViewPage class. What role WebViewPage plays here.
I will really appreciate If you can point me with some good resource to understand the same
1) ViewData is merely a dictionary of objects that you can fill in the Controller and retrieve within the view. Since it is a dictionary of objects you need to cast the data back into the type it was to make full use of it.
2) WebViewPage is the base type of a razor page. It is the defined class which razor pages are compiled into at runtime. The web.config inside the views folder specifies the pageBaseType of the razor pages specifically to WebViewPage. These are two good resources regarding why its use and how you can extend it. Link1 and Link2.
Go peek inside he source code that renders the views
visit msdn

Why is ViewData used when we have ViewBag?

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.

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.

How to do I determine the context of an action in the view (asp.net mvc)

I have a View that contains lots of links, when a user actions one of these links it initiates a filter action on the Controller.
When the View is constructing itself I want to determine which (if any) of the links have been actioned.
I'm overwhelmed with choices, put something in the ViewBag, TempData, or in my ViewModel so that the View can determine the context of the action?
What would be the preferred way?
What would be the preferred way?
ViewModel of course.
ViewBag achieves the same thing as the view model but in a weakly typed manner, so personally I never use it in any of my applications. TempData on the other hand could be considered as a one-redirect-session-storage which I don't see how it would be of any help for your scenario.

How to find out the current resource and controller name from the view in ASP.NET MVC

I'm using the ASP.NET MVC in a RESTful resource manner and I want to be able get the current resource and controller name from the view.
I'm attempt to create a HTML page and I want to know the current resource and controller name is it possible to get this?
you can get that in a view by looking at the ViewContext.Controller property. The ViewContext property also gives access to many other useful properties such as Route Data, Application, Cache, ViewData, etc.
EDIT: To get the actual name of the controller you can go one of two ways:
1) Call GetType() on the Controller property of ViewContext and use that Name property to get the class name of the controller
2) Look at the route data and examine the values for the "controller" key, e.g. ViewContext.RouteData.Values["controller"] (this would likely be the preferred method)
This information is available in the ViewContext property of ViewPage (assuming you are using .aspx for your views).
You can always run GetType while in the controller (or just type it in if thats what you want to do) and store it in the view data. If you are using the same view from multiple controllers, you might want to make it a strongly typed view and have the controller name be part of it.
Can you be more specific with why exactly you want to do this? There might be a better way.

Resources