Reputation: 1
I'm creating an online booking system for my school with mvc 5 in vs. Staff can book themselves into upcoming training programs (which I have called schedules). I created the page using scaffolding method but the issue is the system should not allow a user to book themselves again into the same schedule if they have already made a booking.
EG:
As you see StaffId already exist in the schedule table 12 so the same StaffId cannot book again for the same scheduleID
My code below:
public ActionResult BookingCreate()
{
ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleID");
ViewBag.SessionID = new SelectList(db.TblSessions, "SessionId", "Name");
ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "Firstname");
return View();
}
// POST: Booking/BookingCreate
// To protect from overposting attacks, please enable the specific properties you want to bind to, for more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult BookingCreate([Bind(Include = "BookingID,StaffID,SessionID,ScheduleID,Schedule,TblSession,Staff")] Booking booking)
{
if (ModelState.IsValid)
{
db.Bookings.Add(booking);
db.SaveChanges();
DisplaySuccessMessage("Has append a Booking record");
return RedirectToAction("BookingIndex");
}
ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleID", booking.ScheduleID);
ViewBag.SessionID = new SelectList(db.TblSessions, "SessionId", "Name", booking.SessionID);
ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "Firstname", booking.StaffID);
DisplayErrorMessage();
return View(booking);
}
Upvotes: 0
Views: 1059
Reputation: 53958
You could create a Unique constraint at the Bookings table. Something like the following:
ALTER TABLE Bookings
ADD CONSTRAINT UC_Bookings UNIQUE (ScheduleID, StaffID);
This constraint would impose your business rule at the persistence layer. So any time you post data that violate this rule an exception would be thrown and you could handle it as you wish.
With this approach you always go down in the DB and you accept it or reject the booking.
Another approach, it could be to check (provided that the Model is valid) if any staffID scheduled for the scheduleID you has been reserverd.
if(!db.Bookings.Any(b=>b.ScheduleID == booking.ScheduleID
&& b.StaffID== booking.StaffID))
{
db.Bookings.Add(booking);
db.SaveChanges();
DisplaySuccessMessage("Has append a Booking record");
return RedirectToAction("BookingIndex");
}
The only (evident) drawback of this approach is the fact that you hit twice the database, for each valid booking (I mean a staffID and a scheduleID that have not been used before).
it worked but its giving me a error page is there any way i can change them into any dialog box or text saying something like " you cannot book again" ?
You could try to use a try..catch
.
try
{
db.Bookings.Add(booking);
db.SaveChanges();
DisplaySuccessMessage("Has append a Booking record");
return RedirectToAction("BookingIndex")
}
catch(Exception ex)
{
// Handle here the exception and return to the use a friendly message.
}
Upvotes: 2