View - ( ASP.NET MVC )
In ASP.NET MVC, a view is a graphical representation of data from the model. Views are used to display data to the user in various formats, such as HTML, XML, or JSON. In this page, we will discuss how to create and use views in ASP.NET MVC.
Syntax
Views are created in the Views folder in your ASP.NET MVC project. Each view is associated with a controller action, and its name corresponds to the name of the action. For example, a view named Index.cshtml
is associated with the Index
action in a controller.
Here is the syntax for creating a view:
@model ModelName
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>View Title</title>
</head>
<body>
<header>
<!-- header content -->
</header>
<main>
<!-- main content -->
</main>
<footer>
<!-- footer content -->
</footer>
</body>
</html>
Example
Here's an example of a view for a simple contact form:
@model ContactForm
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Contact Us</title>
</head>
<body>
<header>
<h1>Contact Us</h1>
</header>
<main>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message)
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
}
</main>
<footer>
<p>© 2021 Contact Form</p>
</footer>
</body>
</html>
Output
The output of a view depends on its type. Views can be HTML, XML, or JSON. In the case of an HTML view, the output is an HTML document that is sent to the client's browser.
Explanation
Views are responsible for displaying data to the user in a format that is appropriate for the client's browser. They are created using HTML, XML, or JSON markup, and can include dynamic content generated by the controller.
Use
Views are used to display data to the user in various formats. They are typically created using HTML, XML, or JSON markup and can include dynamic content generated by the controller.
Important Points
- Views are created in the Views folder in your ASP.NET MVC project.
- Each view is associated with a controller action, and its name corresponds to the name of the action.
- Views are responsible for displaying data to the user in a format that is appropriate for the client's browser.
Summary
In this page, we discussed how to create and use views in ASP.NET MVC. We covered the syntax, example, output, explanation, use, important points, and summary of views. Views are an essential part of the ASP.NET MVC pipeline, and understanding how to create and use them is crucial for developing web applications with ASP.NET MVC.