You are on page 1of 2

Action Method

To be considered an action, the method must meet the following requirements:


It must be public.
� It can�t be static.
� It can�t be an extension method.
� It can�t be a constructor, getter, or setter.
� It can�t have open generic types.
� It can�t be a method of the Controller base class.
� It can�t be a method of the ControllerBase base class.
� It can�t contain ref or out parameters.
� It can�t be decorated with the NonAction action selector.
If a method doesn�t meet all these requirements, it isn�t an action method.
ChildActionOnly
The ChildActionOnly attribute prevents the ChildAction method from being exposed as
a web-callable action that can be invoked by a web browser. But it can still be
invoked by making a call to Html.Action() from within a view, as follows:
@Html.Action("ChildAction")
using System.Web.Mvc;
namespace ChildActionSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
[ChildActionOnly]
public ActionResult ChildAction()
{
return View();
}
}
}
Action Selector

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 common use for an action selector is to create an overloaded action to fulfill a


route that differs only by the HTTP method that�s sent to the web server.

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.

We could�ve used Html.RenderPartial("Profile", profile) as well.

The difference is that Html.RenderPartial(...) is a void method that renders the


partial immediately to the
response stream, whereas Html.Partial(...) returns a string and is rendered
immediately in the view.

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.

You might also like