22. Using Controllers with Views pt. 2 Flashcards
What is a view bag?
Action methods can use the view bag to provide a view with extra data. The ViewBag property is inherited from the Controller base class and returns a dynamic object. This allows action methods to create new properties just by assigning values to them. The values assigned to the ViewBag property by the action method are available to the view through a property also called ViewBag. The ViewBag property conveys the object from the action to the view, alongside the view model object.
When should you use the View Bag?
The view bag works best when it is used to provide the view with small amounts of supplementary data without having to create new view model classes for each action method. The problem with the view bag is that the compiler cannot check the use of the properties on dynamic objects, much like views that don’t use an @model expression. It can be difficult to judge when a new view model class should be used, and my rule of thumb is to create a new view model class when the same view model property is used by multiple actions or when an action method adds more than two or three properties to the view bag.
What is temp data?
The temp data feature allows a controller to preserve data from one request to another, which is useful when performing redirections. Temp data is stored using a cookie unless session state is enabled when it is stored as session data. Unlike session data, temp data values are marked for deletion when they are read and removed when the request has been processed.
When values are being stored using the TempData property, a dictionary that is used to store key/value pairs is returned. Since the temp data feature is built on top of the sessions feature, only values that can be serialized to strings can be stored.
Reading a temp data value doesn’t remove it immediately, which means that values can be read repeatedly in the same view. It is only once the request has been processed that the marked values are removed.
What are layouts good for?
The views in applications often contain duplicate elements that deal with setting up the HTML document, defining the head section, loading the Bootstrap CSS file, and so on. Razor supports layouts, which avoid this sort of duplication by consolidating common content in a single file that can be used by any view.
Layouts are typically stored in the Views/Shared folder because they are usually used by the action methods of more than one controller.
The layout contains the common content that will be used by multiple views. The content that is unique to each view is inserted into the response by calling the RenderBody method, which is inherited by the RazorPage class.
What is a layout section?
Allow you to provide regions of content within a layout. Razor sections give greater control over which parts of the view are inserted into the layout and where they are placed.
Sections are defined using the Razor @section expression followed by a name for the section.
Sections can contain the same mix of HTML content and expressions, just like the main part of the view. Sections are applied in a layout with the @RenderSection expression.
What are partial views?
You will often need to use the same set of HTML elements and expressions in several different places. Partial views are views that contain fragments of content that will be included in other views to produce complex responses without duplication.
Partial views are just regular CSHTML files, and it is only the way they are used that differentiates them from standard views.
Partial views are applied by adding a partial element in another view or layout.