Professional Documents
Culture Documents
Лаб2 ВЕБ
Лаб2 ВЕБ
Лабораторна робота №2
З дисципліни: «Веб-технології та веб-дизайн»
Одеса – 2023
Хід роботи
Index.cshtml:
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
ListResponse.cshtml:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ListResponse</title>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
</tr>
@foreach (GuestResponse r in Model)
{
<tr>
<td>@r.Name</td>
<td>@r.Email</td>
<td>@r.Phone</td>
</tr>
}
</table>
</body>
</html>
MyView.cshtml:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>MyView</title>
<link href="~/lib/bootstrap/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="text-center m-2">
<h3> We're going to have party!</h3>
<h4> AAnd you are invited</h4>
<a class="btn btn-primary" asp-action="RsvpForm">RSVP Now</a>
</div>
</body>
</html>
Privacy.cshtml:
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
RsvpForm.cshtml:
@model GuestResponse
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
<link rel="stylesheet" href="~/css/site.css"/>
</head>
<body>
<form asp-action="RsvpForm" method="post">
<div asp-validation-summary="All"></div>
<p>
<label asp-for="Name">Your Name:</label>
<input asp-for="Name" />
</p>
<p>
<label asp-for="Email">Your Email:</label>
<input asp-for="Email" />
</p>
<p>
<label asp-for="Phone">Your Phone:</label>
<input asp-for="Phone" />
</p>
<p>
<label asp-for="WillAttend">Will you attend?</label>
<select asp-for="WillAttend">
<option value="">Choose an option</option>
<option value="true">Yes, I'll be there</option>
<option value="false">No, I can't come</option>
</select>
</p>
<button type="submit">Submit Rsvp</button>
</form>
</body>
</html>
Thanks.cshtml:
@model GuestResponse
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Thanks</title>
</head>
<body>
<h1>
Thank you, @Model.Name
</h1>
@if (Model.WillAttend == "true")
{
@:It`s great
}
else
{
@:Sorry
}
<p>Click <a asp-action="ListResponse">here</a>
</p>
</body>
</html>
Program.cs:
var builder = WebApplication.CreateBuilder(args);
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}");
app.Run();
HomeController.cs:
using lab_web_2.Models;
using Microsoft.AspNetCore.Mvc;
using NuGet.Protocol.Core.Types;
using System.Diagnostics;
namespace lab_web_2.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
return View("MyView");
}
[HttpGet]
public ViewResult RsvpForm()
{
return View();
}
[HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse)
{
if (ModelState.IsValid)
{
Models.Repository.AddResponse(guestResponse);
return View("Thanks", guestResponse);
}
else { return View(); }
}
public ViewResult ListResponse()
{
return View(Models.Repository.Responses.Where(r=>r.WillAttend=="true"));
}
}
}