Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified ContosoPizza/ContosoPizza.db
Binary file not shown.
Binary file added ContosoPizza/ContosoPizza.db-shm
Binary file not shown.
Binary file added ContosoPizza/ContosoPizza.db-wal
Binary file not shown.
7 changes: 4 additions & 3 deletions ContosoPizza/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
ViewData["Title"] = "The Home for Pizza Lovers";
TimeSpan timeInBusiness = DateTime.Now - new DateTime(2018, 8, 14);
}

<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>
<h1 class="display-4">Welcome to Contoso Pizza</h1>
<p class="lead">The best pizza in town for @Convert.ToInt32(timeInBusiness.TotalDays) days!</p>
</div>
72 changes: 72 additions & 0 deletions ContosoPizza/Pages/PizzaList.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@page
@model ContosoPizza.Pages.PizzaListModel
@{
ViewData["Title"] = "Pizza List 🍕";
}

<h1>Pizza List 🍕</h1>

<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="NewPizza.Name" class="control-label"></label>
<input asp-for="NewPizza.Name" class="form-control" />
<span asp-validation-for="NewPizza.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NewPizza.Size" class="control-label"></label>
<select asp-for="NewPizza.Size" class="form-control" id="PizzaSize">
<option value="">-- Select Size --</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>
<span asp-validation-for="NewPizza.Size" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="NewPizza.IsGlutenFree" /> @Html.DisplayNameFor(model =>
model.NewPizza.IsGlutenFree)
</label>
</div>
<div class="form-group">
<label asp-for="NewPizza.Price" class="control-label"></label>
<input asp-for="NewPizza.Price" class="form-control" />
<span asp-validation-for="NewPizza.Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>

<table class="table mt-5">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Size</th>
<th scope="col">Gluten Free</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
@foreach (var pizza in Model.PizzaList)
{
<tr>
<td>@pizza.Name</td>
<td>@($"{pizza.Price:C}")</td>
<td>@pizza.Size</td>
<td>@(pizza.IsGlutenFree ? "✔️" : string.Empty)</td>
<td>
<form method="post" asp-page-handler="Delete" asp-route-id="@pizza.Id">
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
}
</tbody>
</table>

@section Scripts {
<partial name="_ValidationScriptsPartial"/>
}
43 changes: 43 additions & 0 deletions ContosoPizza/Pages/PizzaList.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ContosoPizza.Models;
using ContosoPizza.Services;

namespace ContosoPizza.Pages
{
public class PizzaListModel : PageModel
{
private readonly PizzaService _service;
[BindProperty]
public Pizza NewPizza {get; set;} = default!;
public IList<Pizza> PizzaList {get; set;} = default!;

public PizzaListModel(PizzaService service)
{
_service = service;
}
public void OnGet()
{
PizzaList = _service.GetPizzas();
}

public IActionResult OnPost()
{
if(!ModelState.IsValid || NewPizza == null)
{
return Page();
}

_service.AddPizza(NewPizza);

return RedirectToAction("Get");
}

public IActionResult OnPostDelete(int id)
{
_service.DeletePizza(id);

return RedirectToAction("Get");
}
}
}
3 changes: 3 additions & 0 deletions ContosoPizza/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/PizzaList">Pizza List 🍕</a>
</li>
</ul>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion ContosoPizza/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
builder.Services.AddRazorPages();
builder.Services.AddDbContext<PizzaContext>(options =>
options.UseSqlite("Data Source=ContosoPizza.db"));

builder.Services.AddScoped<PizzaService>();
var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down