net-core
  1. net-core-views

Views - ( ASP.NET Core MVC )

Views are an important part of ASP.NET Core MVC architecture, which is responsible for rendering the HTML markup that will be shown to the user. A view contains the UI elements such as HTML, CSS, and JavaScript to display the data obtained from the respective controller. In this page, we will provide an overview of views in ASP.Net Core MVC.

Syntax

The view file has a .cshtml extension and is located in the Views folder of your ASP.NET Core MVC application.

Razor syntax is used to define the structure of the view. It provides a way to embed C# code snippets into the HTML markup easily.

Here is an example of a Razor view syntax:

@model List<string> // List of strings is used as a model

@{
    ViewData["Title"] = "View Title";
}

<h2>@ViewData["Title"]</h2>

<ul>
@foreach (var item in Model)
{
    <li>@item</li>
}
</ul>

Example

Suppose you have a model class named Employee with the following properties:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Here's an example of a Razor view that displays a list of employees:

@model List<Employee> // List of employees is used as a model

@{
    ViewData["Title"] = "Employees List";
}

<h2>@ViewData["Title"]</h2>

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
    @foreach (var emp in Model)
    {
        <tr>
            <td>@emp.Id</td>
            <td>@emp.Name</td>
        </tr>
    }
    </tbody>
</table>

Output

When the view is rendered, it generates HTML markup based on the model data. In the above example, the output will be an HTML table with two columns, Id and Name, displaying the corresponding data from the model.

Explanation

Views are responsible for rendering HTML markup that can be displayed in the web browser. In ASP.NET Core MVC, views use the Razor syntax to define the structure of the view. Razor provides a way to embed C# code snippets into the HTML markup easily.

You can use various conventions and techniques to pass the data from the controller to the view, such as ViewBag, ViewData, or Strongly-typed models.

Use

Views are essential for creating visually appealing and interactive web applications. They generate the HTML markup that's displayed to the user. Views can contain inline styles, scripts, and dynamic data that's obtained from the respective controller.

Important Points

  • Views are responsible for rendering HTML markup.
  • Razor syntax is used to define the structure of the view.
  • You can use various techniques to pass data from the controller to the view, such as ViewBag, ViewData, or Strongly-typed models.

Summary

In this page, we provided an overview of views in ASP.NET Core MVC, including syntax, example, output, explanation, use, and important points. Views are responsible for rendering HTML markup and displaying web content. They use Razor syntax to define the structure of the page and can contain inline styles, scripts, and dynamic data obtained from the respective controller.

Published on: