\Pages\Restaurants\List.cshtml.cs:918 'HttpPostAttribute' cannot be applied to Razor Page handler methods. Routes for Razor Pages must be declared using the @page directive or using conventions., ну и собсна он мне не исправил ничего:(
@page
@model ASPRestaurants.Pages.Restaurants.ListModel
@{
ViewData["Title"] = "List";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<div>
<form method="get" class="">
<div class="form-group">
<div class="input-group mt-4">
<input name="searchTerm" placeholder="What are you looking for?" type="search" class="form-control border-0 shadow box-shadow" asp-for="SearchTerm"/>
<span class="input-group-btn">
<button class="btn btn-outline-primary border-0 ms-2 shadow box-shadow">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
<path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
</svg>
</button>
</span>
</div>
</div>
</form>
<div class="mt-4 mb-4">
<table class="table table-borderless table-hover rounded box-shadow shadow">
<thead >
<tr class="shadow-sm box-shadow">
<th scope="col">Name</th>
<th scope="col">Location</th>
<th scope="col">Cuisine</th>
</tr>
</thead>
@foreach (var restaurants in Model.Restaurants)
{
<tr class="rounded">
<td>
<a asp-page="./Details" asp-route-restaurantId="@restaurants.Id" class="link-primary text-decoration-none fw-bold m-2">
@restaurants.Name
</a>
</td>
<td>@restaurants.Location</td>
<td>@restaurants.Cuisine</td>
<td class="text-center td-button" style="width: 16px">
<a asp-page="./Edit" asp-route-restaurantId="@restaurants.Id" class="btn btn-sm btn-outline-primary m-1 border-0 shadow box-shadow">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-three-dots" viewBox="0 0 16 16">
<path d="M3 9.5a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3zm5 0a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3zm5 0a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3z"/>
</svg>
</a>
</td>
<td class="text-center td-button" style="width: 16px">
<a @*asp-page="./Delete"*@ data-bs-toggle="modal" data-bs-target="#staticBackdrop" asp-route-restaurantId="@restaurants.Id" class="btn btn-sm btn-outline-primary m-1 border-0 shadow box-shadow">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
<path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
<path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
</svg>
</a>
</td>
</tr>
}
<div class="modal fade" id="deleteModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="deleteModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-static">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete @Model.RestaurantToDelete.Name?
</div>
<div class="modal-footer">
<form method="post">
<button type="submit" class="btn btn-danger">Yes!</button>
<button class="btn btn-primary" data-bs-dismiss="modal" aria-label="Close">Cancel</button>
</form>
</div>
</div>
</div>
</div>
</table>
</div>
<a asp-page="./Edit" class="btn btn-primary">Add New</a>
</div>
using System.Collections.Generic;
using ASPRestaurants.Core;
using ASPRestaurants.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
namespace ASPRestaurants.Pages.Restaurants
{
public class ListModel : PageModel
{
private readonly IConfiguration _configuration;
private readonly IRestaurantData _restaurantData;
public ListModel(IConfiguration configuration, IRestaurantData restaurantData)
{
_configuration = configuration;
_restaurantData = restaurantData;
}
[BindProperty(SupportsGet = true)] public string SearchTerm { get; set; }
public IEnumerable<Restaurant> Restaurants { get; set; }
public Restaurant RestaurantToDelete { get; set; }
public void OnGet()
{
Restaurants = _restaurantData.GetRestaurantsByName(SearchTerm);
}
public IActionResult OnPost(int restaurantId)
{
RestaurantToDelete = _restaurantData.Delete(restaurantId);
_restaurantData.Commit();
if ( RestaurantToDelete == null) RedirectToPage("./NotFound");
TempData["Message"] = $"{ RestaurantToDelete?.Name} deleted";
return RedirectToPage("./List");
}
}
}
public void DeleteCar(string carModel, string color = "", double speed = 0, int yearOfIssue = 0)
{
var query = from c in _cars
where c.CarModel.StartsWith(carModel)
select c;
if (color != string.Empty)
query = query.Where(cl => cl.Color == color);
if (speed > 0)
query = query.Where(sp => Math.Abs(sp.Speed - speed) < 0.2);
if (yearOfIssue > 0)
query = query.Where(y => y.YearOfIssue == yearOfIssue);
var results = query.Select(f => f).ToList();
foreach (var car in results) _cars.Remove(car);
}