@* Model of the view type declaration: *@
@model IEnumerable<RoleModel>
@*
The type of the model object of this view is IEnumerable<RoleModel> and this object can be used by writing "Model" within Razor syntax.
*@
@* Generated from Custom Template. *@
@* Model namespace using directive should be added to _ViewImports.cshtml. *@
@{
var containerDivClass = "container"; // Bootstrap CSS class, can be set to "container-fluid" for full width
var elementCount = Model.Count(); // Count method returns the element count of an IEnumerable collection.
// Count property returns the element count of a List collection.
@* C# Ternary Operator usage: *@
var recordCount = elementCount == 0 ? "No records found." : elementCount == 1 ? "1 record found." : $"{elementCount} records found.";
ViewData["Title"] = "Role List"; // "Role List" value will be written in the h1 tag below and Views/Shared/_Layout.cshtml view's title tag.
}
<div class="@containerDivClass"> @* The string value set above will be written in the div tag's class HTML attribute. *@
<h3>@ViewData["Title"]</h3> @* The ViewData["Title"] value set above will be written in the h1 tag. *@
<hr />
</div>
@if (Model is not null) @* for preventing Null Reference Exception *@
{
@* generally we don't need the form tag since we are displaying Model items, however it is created for future usage of posting data to the action if needed *@
@* HTML: *@
@*<form action="/Roles/Index" method="post">*@ @* default HTTP Method of the HTML form tag is get *@
@*
HTML Helper: Action name, controller name and form method (FormMethod.Post) may be written as parameters.
If parameters are not given, the form will be submitted to the related controller's related post action.
The ending curly brace must be used below for ending the form tag.
Default HTTP Method of the BeginForm HTML Helper is post.
*@
@* @using (Html.BeginForm())
{ *@
@*
Tag Helper: asp-controller="Roles" can also be written after asp-action="Index". It is optional since we are posting data to an action of the same controller.
Default HTTP Method of the Form Tag Helper is post. autocomplete="off": disables the usage of browser's data cache for this form.
Form data will be posted to the specified controller's action if the action with HttpPost action method exists in the controller,
otherwise data will be posted to the action with HttpGet action method. Optionally method="post" can also be written after asp-action="Index".
Method can be specified in the form HTML Helper (FormMethod.Post or FormMethod.Get) and Tag Helper (method="post" or method="get") to send the data
to the action with specified action method (HttpGet or HttpPost).
*@
<form asp-action="Index" autocomplete="off">
<div class="@containerDivClass">
<div class="row">
<div class="col-12 text-danger">
@TempData["Message"] @* for displaying the operation result if set in an action *@
</div>
</div>
<div class="row mb-3">
<div class="col-10 text-success">
@* Way 1: *@
@* @Model.Count() record(s) found. *@
@* Way 2: using the recordCount variable we set above for displaying the Model's element count *@
@recordCount
</div>
<div class="col-2 text-end">
@* HTML: *@
@* <a href="/Roles/Create">Create</a> *@
@*
HTML Helper: controller name may also be provided as the third parameter after the second action name parameter,
it is optional since we are redirecting to an action of the same controller
*@
@* @Html.ActionLink("Create", "Create") *@
@*
Tag Helper: asp-controller="Roles" can also be written after asp-action="Create",
it is optional since we are redirecting to an action of the same controller
*@
<a asp-action="Create">Create</a>
</div>
</div>
<table class="table table-striped table-hover">
<thead class="table-secondary">
<tr>
<th>
@* HTML: *@
@* Role Name *@
@*
HTML Helper: uses the DisplayName attribute of the Name property in the RoleModel class,
HTML Helpers ending with "For" are generally used with the model delegate.
*@
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.UserCount) @* property for formatted or relational data in the model *@
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) { // iteration of each element in the collection of type RoleModel and displaying its property value
<tr>
<td>
@* Way 1: *@
@* @item.Name *@
@* Way 2: HTML Helper: writes the value of the Name property in the RoleModel class *@
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserCount) @* property for formatted or relational data in the model *@
</td>
<td class="text-end w-25">
@*
HTML:
Will render the example: <a href="/Roles/Details/17">Details</a>.
Details action can also be linked by using a query string: "/Roles/Details?id=17" in the href attribute.
17 is the id route value.
*@
@* HTML Helper: last parameter is the route value which must have the same property name as action's parameter name (id) *@
@* @Html.ActionLink("Details", "Details", new { id = item.Record.Id }) *@
@* Tag Helper: asp-route-id attribute is the route value which, after asp-route-, must be the same as action's parameter name (id) *@
<a asp-action="Details" asp-route-id="@item.Record.Id">Details</a> |
<a asp-action="Edit" asp-route-id="@item.Record.Id">Edit</a> |
<a asp-action="Delete" asp-route-id="@item.Record.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
@* } *@ @* for ending the form tag created by the BeginForm HTML Helper *@
</form>
}