NET 6 - Razor Page

Last modified: March 26, 2022
  • Select TicketingSystem.RazorPages project
  • Add Event folder to Pages folder

page

page

page

Index.cshtml

@page @model TicketingSystem.RazorPages.Pages.Event.IndexModel @{ } <h1>Index</h1> <p> <a asp-page="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> Name </th> <th> Location </th> <th> Date </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model.events) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Location) </td> <td> @Html.DisplayFor(modelItem => item.Date) </td> <td> <a asp-page="Edit" asp-route-id="@item.Id" >Edit</a> </td> </tr> } </tbody> </table>

Index.cshtml.cs

using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using TicketingSystem.Service.Services; using TicketingSystem.Service.ViewModels; namespace TicketingSystem.RazorPages.Pages.Event { public class IndexModel : PageModel { private readonly EventService _eventService; public IndexModel(EventService eventService) { _eventService = eventService; } [BindProperty] public List<EventViewModel> events { get; set; } public async Task OnGetAsync() { events = await _eventService.GetAllAsync(); } } }

Create.cshtml

@page @model TicketingSystem.RazorPages.Pages.Event.CreateModel @{ } <h1>Create</h1> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> Name: <input name="Name" class="form-control" /> </div> <div class="form-group"> Location <input name="Location" class="form-control" /> </div> <div class="form-group"> Date <input name="Date" type="datetime-local" class="form-control" /> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </form> </div> </div>

Create.cshtml.cs

using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using TicketingSystem.Service.Services; using TicketingSystem.Service.ViewModels; namespace TicketingSystem.RazorPages.Pages.Event { public class CreateModel : PageModel { private readonly EventService _eventService; public CreateModel(EventService eventService) { _eventService = eventService; } [BindProperty] public EventViewModel _event { get; set; } public void OnGet() { } public async Task<RedirectResult> OnPostAsync() { await _eventService.Add(_event); return Redirect("~/Event/"); } } }

Edit.cshtml

@page @model TicketingSystem.RazorPages.Pages.Event.EditModel @{ } <h1>Edit</h1> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Edit"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> Name: <input name="Name" value="@Model._event.Name" class="form-control" /> </div> <div class="form-group"> Location <input name="Location" value="@Model._event.Location" class="form-control" /> </div> <div class="form-group"> Date <input name="Date" type="datetime-local" value="@Model._event.Date" class="form-control" /> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </form> <br/> </div> </div>

Edit.cshtml.cs

using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using TicketingSystem.Service.Services; using TicketingSystem.Service.ViewModels; namespace TicketingSystem.RazorPages.Pages.Event { public class EditModel : PageModel { private readonly EventService _eventService; public EditModel(EventService eventService) { _eventService = eventService; } [BindProperty] public EventViewModel _event { get; set; } public async Task OnGetAsync(string id) { _event = await _eventService.GetById(id); } public async Task<IActionResult> OnPostAsync() { var _e = await _eventService.GetById(_event.Id); if (_e == null) return NotFound(); _e.Location = _event.Location; _e.Name = _event.Name; _e.Date = _event.Date; await _eventService.Update(_e); return Redirect("~/Event/"); } } }
Repeat above technique to do for EventBooking