Professional Documents
Culture Documents
The action selector is used to control which action method is selected to handle a
particular route.
There are a number of built-in action selectors, each used to filter down the
actions.
A concrete example of this is to have two action methods named �Edit�. One would
have the HttpGetAttribute applied and would render an edit form to the browser, and
the other would have the HttpPostAttribute applied and would take a view model as a
parameter.
Layouts
A layout defines a common template, leaving placeholders for derived pages or other
layouts to fill in the blanks.
the layout defines placeholders for both a page title and main content. Layouts can
also nest within each other.
Layouts are best applied when multiple views share common content.
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Alternatively, we can specify the layout globally, inside a special
_ViewStart.cshtml file.
PartIals
If you find yourself copying and pasting one snippet of HTML from one view to the
next, that snippet is a great candidate for a partial.
The mechanism for rendering a partial is quite simple. We can use the RenderPartial
method or the Partial method in a parent view as shown here.
@model IEnumerable<Profile>
<h2>Profiles</h2>
<table>
<tr>
<th>Username</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
</tr>
@foreach (var profile in Model) {
@Html.Partial("_Profile", profile)
}
</table>
For each row, we want to define a partial to render a single row.
The RenderPartial method takes a partial name and an optional model. In order to
prevent accidentally using a partial view from an action, we prefix the view name
with an underscore.
By default, partials do not get the _ViewStart defaults applied, meaning that no
layout is used. However, we can still specify a layout in our partial if needed.
We can develop strongly typed partials with the same access to the strongly typed
view helpers by specifying a model, as follows.Rendering a partial from
a parent view
@model AccountProfile.Models.Profile
<tr>
<td>@Model.FirstName</td>
<td>@Model.LastName</td>
<td>@Model.Email</td>
</tr>
Partials work well for displaying common snippets of content for information
already in the main model from the controller action. But for other widgets, we
need to look at the ASP.NET MVC feature called child actions.Rendering a
Partial view is a view which can be rendered from another view which is called
parent view or it can also be called as individual page.