Unit 3
Unit 3
NET Core - I
#2101CS511
Unit-3
Views, Helpers &
Routing
Searching View
Behavior
The default behavior of the View method (i.e., return View();) is to return a view with the same name as
the action method name from which it is being called.
For example, the Index ActionResult method of the Home controller is used to search for a view file with
the name Index.cshtml in the following two locations.
1. First, it will look for the “Index.cshtml” file within the /Views/Home, i.e., Views/[ControllerName]
folder, as the action method belongs to Home Controller.
2. Then it will look for the “Index.cshtml” file in the “/Views/Shared/” folder.
3. Then it will look for the “Index.cshtml” file in the “/Pages/Shared/” folder.
@Html.CheckBox("Checkbox1", false)
CheckBox <input id="Checkbox1" name="Checkbox1" type="checkbox" value="true" />
<input name="myCheckbox" type="hidden" value="false" />
@Html.CheckBoxFor(m=>m.IsDetained)
CheckBoxFor <input id="Checkbox1" name="Checkbox1" type="checkbox" value="true" />
<input name="myCheckbox" type="hidden" value="false" />
@Html.RadioButtonFor(m=>m.IsDetained, "val")
RadioButtonFor
<input checked="checked" id="Radiobutton1" name="Radiobutton1" type="radio" value="val" />
Get a link to specific controller and action and set css class for the
link
1 @Html.ActionLink("Link to Index action of Home controller with id=100500 and
2 name='John Doe' and CSS class", "Index", "Home",
3 new { id = 100500, name = "John Doe" },
4 new { @class="css-class"})
In the above line, all the Tag helpers will be imported which are mentioned in
'Microsoft.AspNetCore.Mvc.TagHelpers' assembly.
Whatever is referenced/imported inside _ViewImports.cshtml will be available for all the
views.
#2101CS511 (ASP.NET Core - I) Unit -3 Views, Helpers &
Prof. Naimish R. Vadodariya 43
Common Built-in Tag Helpers
These Tag Helpers are provided by default as part of the ASP.NET Core framework.
They cover common scenarios and tasks, making generating HTML with dynamic behavior
easier, such as generating links, creating forms, loading assets, showing validation messages,
etc. Some examples of built-in Tag Helpers include:
<a asp-controller=”Home” asp-action=”Index”>Link</a>: Generates a link with a URL
that’s generated by routing based on the specified controller and action.
<img src=”~/images/pic.jpg” asp-append-version=”true” />: Generates a <img> tag with
a cache-busting query string to ensure the latest version of the image is fetched.
<form asp-controller=”Account” asp-action=”Login”
method=”post”>…</form>: Generates a form element with the appropriate action attribute
for the specified controller and action.
<input asp-for=”Username” />: Generates an input field with attributes based on the model
property specified by asp-for.
<span asp-validation-for="Username" class="text-danger"></span>: Generates a
username validation message with specified control.
#2101CS511 (ASP.NET Core - I) Unit -3 Views, Helpers &
Prof. Naimish R. Vadodariya 44
Built-in Tag Helpers Cont..
Attribute Asp Attribute Name Description
action asp-action The name of the action method on an MVC controller
all-route-data asp-all-route-data Multiple route parameter values
area asp-area The name of the Area
Controller asp-controller The name of the MVC controller
Fragment asp-fragment The fragment in the URL
host asp-host The domain
page asp-page The Razor page to link to
page-handler asp-page-handler The Razor page handler method to invoke
protocol asp-protocol The protocol (http, https, ftp etc)
route asp-route The name of the route
route- asp-route-* A single route parameter value
Now, the question that should come to your mind is, we have not explicitly defined any
routing rules for the application, then how does this mapping is done i.e.
How the /Home/Index URL is mapped to the Index action method and
how /Home/Details/2 URL is mapped to the Details action method of the Home Controller
class.
Prof. Naimish R. Vadodariya
#2101CS511 (ASP.NET Core - I) Unit -3 Views, Helpers &
51
Program.cs
The default route is
created with the
following URL
Pattern.
So, if we don’t specify
anything in the URL,
then by default the
Index action method
of the Home
Controller class is
going to handle the
This is Required for Routing
request.
{controller=Home}/
This will define the route
pattern along with the Default {action=Index}/{id?}
Route
Outpu
t
Thank
You