Professional Documents
Culture Documents
Lesson15 - Exercises (PS11-12 Hoh Jungi)
Lesson15 - Exercises (PS11-12 Hoh Jungi)
Lesson15Ex1
Filename: .\Pages\Index.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome Jungi</h1>
<p><a href="Calculator">Jungi's Calculator</a></p>
<p><a href="BookOrder">Jungi's Book Order</a></p>
<p> <a href="BubbleTea">Jungi's Bubble Tea</a></p>
<p> <a href="BurgerShop">Jungi's Burger Shop</a></p>
<p><a href="Cafe">Jungi's Cafe</a></p>
<p><a href="Reservation">Dinner Reservation</a></p>
<p><a href="Appointment">Medical Appointment</a></p>
</div>
After filling in all of the text boxes except that Name is not supplied, and Age is invalid but click OK
After filling in all of the text boxes except that the Appointment Date/Time is too early but click OK
After filling in all of the text boxes with and all validation criteria satisfied
Filename: .\Pages\Appointment.cshtml
@page
@model RazorPagesBasics.Pages.AppointmentModel
@{
ViewData["Title"] = "Medical Appointment";
}
<h1>Medical Appointment</h1>
<div asp-validation-summary="All"></div>
<form method="post">
<p>
Name <input type="text" asp-for="Name" />
Age (in years) <input type="text" asp-for="Age" />
</p>
<p>
Appointment Date/Time <input type="datetime-local" asp-for="AppointmentDateTime" asp-
format="{0:yyyy-MM-ddTHH:mm}" />
</p>
<p>
<i style="color : red">@Model.ErrorMessage</i>
</p>
<p>
<table class="table">
<thead>
<tr><th>Doctor</th><th>Consultation Fee</th></tr>
</thead>
<tbody>
<tr><td><input type="radio" asp-for="Consult" value="Senior Consultant" checked='checked'> Senior
Consultant</td><td>$200.00</td></tr>
<tr><td><input type="radio" asp-for="Consult" value="Consultant">
Consultant</td><td>$120.00</td></tr>
</tbody>
</table>
</p>
<p>
<input type="checkbox" asp-for="Fever"> Do you currently have any fever symptoms? </br>
<input type="checkbox" asp-for="Overseas"> Have you travelled overseas in the past 14 days? </br>
</p>
<p><input type="submit" value="OK" /></p>
<p>Patient Info: @Model.PatientInfo</p>
<p>Doctor Info: @Model.DoctorInfo</p>
<p>Appointment Date/Time: @Model.strAppointmentDateTime</p>
</form>
Filename: .\Pages\Appointment.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
namespace RazorPagesBasics.Pages
{
public class AppointmentModel : PageModel
{
[BindProperty]
[Required(ErrorMessage = "Name cannot be left empty.")]
public string Name { get; set; }
[BindProperty]
[Required(ErrorMessage = "Age must be between 1 to 16.")]
[Range(1, 16, ErrorMessage = "Age must be between 1 to 16.")]
public int Age { get; set; }
[BindProperty]
public DateTime AppointmentDateTime
{ get; set; } = System.DateTime.Now;
[BindProperty]
public string Consult { get; set; }
[BindProperty]
public bool Fever { get; set; }
[BindProperty]
public bool Overseas { get; set; }
public string PatientInfo { get; set; }
public string DoctorInfo { get; set; }
public string ErrorMessage { get; set; }
public string strAppointmentDateTime { get; set; }
public IActionResult Onpost()
{
if (ModelState.IsValid)
{
strAppointmentDateTime = string.Format("{0:dd/MM/yyyy hh:mm tt}", AppointmentDateTime);
if (AppointmentDateTime < System.DateTime.Now.AddHours(3))
{
ErrorMessage = "Appointment Date/Time must be at least 3 hours in advance";
}
else
{
PatientInfo = Name + ", " + Age + " years";
if (Fever == true)
{
PatientInfo += ", Fever";
}
if (Overseas == true)
{
PatientInfo += ", Travelled overseas";
}
if (Consult == "Consultant")
{
DoctorInfo = "Consultant, $120.00";
}
else
{
DoctorInfo = "Senior Consultant, $200.00";
}
}
return Page();
}
else
{
return Page();
}
}
}
}