I’m working on an ASP.NET MVC application and I’m facing a challenge with deleting records. Here’s the specific scenario:
I have two database tables: AvailableDates and Bookings.
In the UI, users can view available dates.
When a user selects a date for deletion, the system should delete the corresponding record from Bookings table (including the selected date, submitted date, and username).
I’ve implemented a “Confirm Cancellation” button to initiate the deletion process. Clicking this button results in a 404 error: “This localhost page can’t be found. No webpage was found for the web address: https://localhost:44355/Booking/ConfirmCancelBooking”.
How can I resolve the 404 error and ensure the ConfirmCancelBooking action handles the deletion process correctly?
Models:
public class AvailableDate
{
public int Id { get; set; }
public DateTime Date { get; set; }
}
public class Booking
{
public int Id { get; set; }
public string Username { get; set; }
public DateTime SelectedDate { get; set; }
public DateTime SubmissionDate { get; set; }
}
Controller:
public class BookingController : Controller
{
private readonly MyDbContext _context;
public BookingController(MyDbContext context)
{
_context = context;
}
public IActionResult CancelBooking(int bookingId)
{
string fullUsername = User.Identity.Name;
var booking = _context.Bookings.SingleOrDefault(b => b.Id == bookingId && b.Username == fullUsername);
if (booking == null)
{
return NotFound();
}
return View(booking);
}
[HttpPost]
public IActionResult ConfirmCancelBooking(int bookingId)
{
string fullUsername = User.Identity.Name;
var booking = _context.Bookings.SingleOrDefault(b => b.Id == bookingId && b.Username == fullUsername);
if (booking == null)
{
return NotFound();
}
_context.Bookings.Remove(booking);
_context.SaveChanges();
return RedirectToAction("Index");
}
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.