Views
handles the app's data presentation and user interaction
Layouts
Most web apps have a common layout that provides the user with a consistent experience as they navigate from page to page. The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer.
Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app. All of these shared elements may be defined in a _Layout.cshtml
file, which can then be referenced by any view used within the app. Layouts reduce duplicate code in views.
Partial View
A partial view is a Razor markup file (.cshtml
) without an @page
directive that renders HTML output within another markup file's rendered output.
Partial views are an effective way to:
Break up large markup files into smaller components.
In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view. The code in the markup file is manageable because the markup only contains the overall page structure and references to partial views.
Reduce the duplication of common markup content across markup files.
When the same markup elements are used across markup files, a partial view removes the duplication of markup content into one partial view file. When the markup is changed in the partial view, it updates the rendered output of the markup files that use the partial view.
Use a partial view in a markup file
Partial views shouldn't be used to maintain common layout elements. Common layout elements should be specified in _Layout.cshtml
files.
Don't use a partial view where complex rendering logic or code execution is required to render the markup. Instead of a partial view, use a view component.
pass data to views
strongly typed data - ViewModel
weekly typed data -
ViewData
(ViewDataAttribute
),ViewBag
strongly typed data
pass an instance of the viewmodel type to the view from the action which allows the view to take advantage of strong type checking.
Usually, ViewModels are Plain Old CLR Object (POCO) classes with little or no behavior (methods) defined.
weekly typed data
ViewData
ViewData
ViewData
is aViewDataDictionary
object accessed throughstring
keys.You can use
ViewData
to pass data from controllers to views and within views, including partial views and layouts.
[ViewData]
attribute
[ViewData]
attributeAnother approach that uses the
ViewDataDictionary
isViewDataAttribute
.Properties on controllers or Razor Page models marked with the
[ViewData]
attribute have their values stored and loaded from the dictionary.
ViewBag
ViewBag
The
ViewBag
property is a wrapper aroundViewData
that provides dynamic properties for the underlyingViewData
collection using dot notation.ViewBag
can be more convenient to work with, since it doesn't require casting.Simpler to check for null values. Example:
@ViewBag.Person?.Name
Strong typing (or strongly typed) means that every variable and constant has an explicitly defined type (for example, string
, int
, or DateTime
).
Unlike strong types, weak types (or loose types) means that you don't explicitly declare the type of data you're using.
Last updated