Online Restaurant Website Part 24 Table Reservation

   Online Restaurant Website Part 24 Table Reservation

Hi, Dear's here we learn how to implement PizzaRestaurantDrink Website in Visual Studio using C# ASP.NET MVC. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials




In this video we are going to implement Table Reservation Designing + Implementation for more details click here: watch vedio

Note : open you sql server where database is allocated, and execute below code 

SET IDENTITY_INSERT [dbo].[BookingStatusTable] ON 
GO
INSERT [dbo].[BookingStatusTable] ([BookingStatusID], [BookingStatus]) VALUES (1, N'Processing')
GO
INSERT [dbo].[BookingStatusTable] ([BookingStatusID], [BookingStatus]) VALUES (2, N'Approved')
GO
INSERT [dbo].[BookingStatusTable] ([BookingStatusID], [BookingStatus]) VALUES (3, N'Reject')
GO
INSERT [dbo].[BookingStatusTable] ([BookingStatusID], [BookingStatus]) VALUES (4, N'Canceled')
GO
SET IDENTITY_INSERT [dbo].[BookingStatusTable] OFF
GO

First we are going to create TableReservationMV for data model to show data in view. code add below:

> TableReservationMV Model Code : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PizzaRestaurantDrink.Models
{
    public class TableReservationMV
    {
        public int BookingTableID { get; set; }
        public string BookingUserName { get; set; }
        public string FullName { get; set; }
        public string EmailAddress { get; set; }
        public string MobileNo { get; set; }
        public System.DateTime BookingDate { get; set; }
        public System.DateTime ReservationDateTime { get; set; }
        public int NoOfPersons { get; set; }
        public string ProcessBy_User { get; set; }
        public string BookingStatus { get; set; }
        public string Description { get; set; }
    }
}

> CRU_TableReservationMV Model Code :  use to create and update table reservation record in database

using Dblayer;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PizzaRestaurantDrink.Models
{
    public class CRU_TableReservationMV
    {
        PizzaRestaurentandDrinksDbEntities db = new PizzaRestaurentandDrinksDbEntities();
        public CRU_TableReservationMV(){}
        public CRU_TableReservationMV(int usertypeid, int userid, int id = 0)
        {
            GetReservationList(usertypeid, userid);
            if (userid > 0)
            {
                var user = db.UserTables.Find(userid);
                FullName = user.FirstName + " " + user.LastName;
                EmailAddress = user.EmailAddress;
                MobileNo = user.ContactNo;
            }
            ReservationDate = null;
            ReservationTime = null;
            var edit = db.BookingTblTables.Find(id);
            if (edit != null)
            {
                BookingTableID = edit.BookingTableID;
                FullName = edit.FullName;
                EmailAddress = edit.EmailAddress;
                MobileNo = edit.MobileNo;
                ReservationDate = edit.ReservationDateTime.Date;
                ReservationTime = edit.ReservationDateTime.ToString("hh:mm:ss tt");
                NoOfPersons = edit.NoOfPersons;
                BookingStatusID = edit.BookingStatusID;
                Description = edit.Description;
            }
            else
            {
                BookingTableID = 0;
                FullName = string.Empty;
                EmailAddress = string.Empty;
                MobileNo = string.Empty;
                ReservationDate = null;
                ReservationTime = string.Empty;
                NoOfPersons = null;
                BookingStatusID = 0;
                Description = string.Empty;
            }
        }
        public int BookingTableID { get; set; }
        [DataType(DataType.Text)]
        public string FullName { get; set; }
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }
        public string MobileNo { get; set; }
        [DataType(DataType.Date)]
        public System.DateTime BookingDate { get; set; }
        public System.DateTime? ReservationDate { get; set; }
        public string ReservationTime { get; set; }
        public int? NoOfPersons { get; set; }
        public int BookingStatusID { get; set; }
        public string Description { get; set; }

        public List<TableReservationMV> ReservationList { get; set; }
        public void GetReservationList(int usertypeid, int userid)
        {
            ReservationList = new List<TableReservationMV>();
            if (usertypeid == 4) // Customer
            {
                var list = db.BookingTblTables.Where(u => u.BookingUserID == userid).ToList();
                if (list.Count > 0)
                {
                    list.OrderByDescending(o => o.BookingTableID).ToList();
                }
                foreach (var item in list)
                {
                    var bookingusername = item.UserTable.UserName;

                    var processbyuser = item.ProcessBy_UserID > 0 ?
                        db.UserTables.Find(item.ProcessBy_UserID).UserName :
                        string.Empty;

                    var bookingstatus = item.BookingStatusTable.BookingStatus;
                    ReservationList.Add(new TableReservationMV
                    {
                        BookingTableID = item.BookingTableID,
                        BookingUserName = bookingusername,
                        FullName = item.FullName,
                        EmailAddress = item.EmailAddress,
                        MobileNo = item.MobileNo,
                        BookingDate = item.BookingDate,
                        ReservationDateTime = item.ReservationDateTime,
                        NoOfPersons = item.NoOfPersons,
                        ProcessBy_User = processbyuser,
                        BookingStatus = bookingstatus,
                        Description = item.Description
                    });
                }
            }
            else if (usertypeid == 1 || usertypeid == 2 || usertypeid == 3)
            {
                var list = db.BookingTblTables.ToList();
                if (list.Count > 0)
                {
                    list.OrderByDescending(o => o.BookingTableID).ToList();
                }
                foreach (var item in list)
                {
                    var bookingusername = item.UserTable.UserName;

                    var processbyuser = item.ProcessBy_UserID > 0 ?
                        db.UserTables.Find(item.ProcessBy_UserID).UserName :
                        string.Empty;

                    var bookingstatus = item.BookingStatusTable.BookingStatus;
                    ReservationList.Add(new TableReservationMV
                    {
                        BookingTableID = item.BookingTableID,
                        BookingUserName = bookingusername,
                        FullName = item.FullName,
                        EmailAddress = item.EmailAddress,
                        MobileNo = item.MobileNo,
                        BookingDate = item.BookingDate,
                        ReservationDateTime = item.ReservationDateTime,
                        NoOfPersons = item.NoOfPersons,
                        ProcessBy_User = processbyuser,
                        BookingStatus = bookingstatus,
                        Description = item.Description
                    });
                }
            }
        }
    }
}

Now create TableReservation controller first to create Table Reservation  Actions

> Table Reservation Controller  Code : 

using Dblayer;
using PizzaRestaurantDrink.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PizzaRestaurantDrink.Controllers
{
    public class TableReservationController : Controller
    {
        PizzaRestaurentandDrinksDbEntities Db = new PizzaRestaurentandDrinksDbEntities();
    }
}

BookTable  Action Code :  Add BookTable Action code in Table Reservation Controller

[HttpPost]
        public ActionResult BookTable(CRU_TableReservationMV cRU_TableReservationMV)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }
            int userid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            if (ModelState.IsValid)
            {
                var date = Convert.ToDateTime(cRU_TableReservationMV.ReservationDate).Date.ToString("yyyy/MM/dd");
                var time = cRU_TableReservationMV.ReservationTime;
                var reservationdatetime = Convert.ToDateTime(date + " " + time);
                var reservation = new BookingTblTable();
                reservation.BookingUserID = userid;
                reservation.FullName = cRU_TableReservationMV.FullName;
                reservation.EmailAddress = cRU_TableReservationMV.EmailAddress;
                reservation.MobileNo = cRU_TableReservationMV.MobileNo;
                reservation.BookingDate = DateTime.Now;
                reservation.ReservationDateTime = reservationdatetime;
                reservation.NoOfPersons = (int)cRU_TableReservationMV.NoOfPersons;
                reservation.ProcessBy_UserID = 0;
                reservation.BookingStatusID = 1;
                reservation.Description = string.Empty;
                Db.BookingTblTables.Add(reservation);
                Db.SaveChanges();
                return RedirectToAction("Index","Home");
            }
            return View(cRU_TableReservationMV);
        }
    }

Next we are going to create partial view for booking table 'Partial_TableReservation View', code show below 

>  Partial_TableReservation View Code : 

@model PizzaRestaurantDrink.Models.CRU_TableReservationMV
<!-- Reservation Start -->
<div class="reservation" id="reservationtable">
    <div class="container">
        <div class="row ">
            <!-- Title Content Start -->
            <div class="col-sm-12 commontop white text-center">
                <h4>Book Your Table</h4>
                <div class="divider style-1 center">
                    <span class="hr-simple left"></span>
                    <i class="icofont icofont-ui-press hr-icon"></i>
                    <span class="hr-simple right"></span>
                </div>
                <p>Decide on a date and time: Choose the date and time you want to visit the restaurant, keeping in mind the restaurant's peak hours and availability.</p>
            </div>
            <!-- Title Content End -->
            <div class="col-md-12 col-12">
                <!-- Reservation Form Start -->
                @using (Html.BeginForm("BookTable", "TableReservation", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "row reservation-form", novalidate = "novalidate" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    @Html.HiddenFor(model => model.BookingTableID)
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-ui-user"></i>
                        @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control", @placeholder = "name" } })
                        @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-ui-message"></i>
                        @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control", @placeholder = "email" } })
                        @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-phone"></i>
                        @Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "mobile number" } })
                        @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-ui-calendar"></i>
                        @Html.EditorFor(model => model.ReservationDate, new { htmlAttributes = new { @class = "form-control", @placeholder = "booking date (yyyy/mm/dd)" } })
                        @Html.ValidationMessageFor(model => model.ReservationDate, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-clock-time"></i>
                        @Html.EditorFor(model => model.ReservationTime, new { htmlAttributes = new { @class = "form-control", @placeholder = "booking time (hh:mm AM|PM)" } })
                        @Html.ValidationMessageFor(model => model.ReservationTime, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-md-4 col-sm-6">
                        <i class="icofont icofont-users"></i>
                        @Html.EditorFor(model => model.NoOfPersons, new { htmlAttributes = new { @class = "form-control", @placeholder = "number of persons" } })
                        @Html.ValidationMessageFor(model => model.NoOfPersons, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group col-12 col-md-12">
                        <div class="">
                            <div id="emailSend" class="alert alert-success" role="alert" style="display: none;">
                                <div class="success-text">Your Message has been successfully sent.</div>
                            </div>
                            <div id="emailError" class="alert alert-danger" role="alert" style="display: none;">
                                <div class="alert-text">Server error <br> Try again later.</div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group col-12 col-md-12">
                        <div class="text-center">
                            <button type="submit" class="btn btn-theme btn-wide">book now</button>
                        </div>
                    </div>

                }
                <!-- Reservation Form End -->
            </div>
        </div>
    </div>
</div>
<!-- Reservation End  -->

Next we are going to add Partial_TableReservation partial view to Index view inside in Home Controller, code show below 

>  Add to Index View Code : ~/Home/Index

<!-- Reservation Start -->
@{
    int userid = 0;
    int usertypeid = 0;
    int.TryParse(Convert.ToString(Session["UserID"]), out userid);
    int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
    Html.RenderPartial("~/Views/TableReservation/Partial_TableReservation.cshtml", new PizzaRestaurantDrink.Models.CRU_TableReservationMV(usertypeid, userid));
}
<!-- Reservation End  -->

Next we are going to create Action in Table Reservation Controller for getting BookingTblTable record, code show below 

>  BookingTables Action Code : Add to TableReservationController

 public ActionResult BookingTables(int id)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }
            int userid = 0;
            int usertypeid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
            var list =  new CRU_TableReservationMV(usertypeid, userid,id);
            ViewBag.BookingStatusID = new SelectList(Db.BookingStatusTables.ToList(), "BookingStatusID", "BookingStatus", "0");
            return View(list);
        }

[HttpPost]
        public ActionResult BookingTables(CRU_TableReservationMV cRU_TableReservationMV)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }
            int userid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            if (ModelState.IsValid)
            {
                var date = Convert.ToDateTime(cRU_TableReservationMV.ReservationDate).Date.ToString("yyyy/MM/dd");
                var time = cRU_TableReservationMV.ReservationTime;
                var reservationdatetime = Convert.ToDateTime(date + " " + time);
                var reservation = Db.BookingTblTables.Find(cRU_TableReservationMV.BookingTableID);
                reservation.ProcessBy_UserID = userid;
                reservation.BookingStatusID = cRU_TableReservationMV.BookingStatusID;
                reservation.Description = cRU_TableReservationMV.Description;
                Db.Entry(reservation).State = System.Data.Entity.EntityState.Modified;
                Db.SaveChanges();
                return RedirectToAction("BookingTables",new { id = 0});
            }
            return View(cRU_TableReservationMV);
        }

>  BookingTables View Code : 

@model PizzaRestaurantDrink.Models.CRU_TableReservationMV
@{
    ViewBag.Title = "Table Reservation";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
    <div class="container">
        <div class="matter">
            <h2>Table Reservation</h2>
            <ul class="list-inline">
                <li class="list-inline-item"><a href="@Url.Content("~/Home/Index")">HOME</a></li>
                <li class="list-inline-item"><a href="@Url.Content("~/TableReservation/BookingTables")">Table Reservation</a></li>
            </ul>
        </div>
    </div>
</div>
<!-- Breadcrumb End -->
<div class="contactus">
    <div class="container">
        <div class="row">
            <!-- Title Content Start -->
            <div class="col-sm-12 commontop text-center">
                <h4>Table Reservation</h4>
                <div class="divider style-1 center">
                    <span class="hr-simple left"></span>
                    <i class="icofont icofont-ui-press hr-icon"></i>
                    <span class="hr-simple right"></span>
                </div>
            </div>
            <!-- Title Content End -->
            @{
                int userid = 0;
                int usertypeid = 0;
                int.TryParse(Convert.ToString(Session["UserID"]), out userid);
                int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
                if (usertypeid == 1 || usertypeid == 2 || usertypeid == 3)
                {
                    if (Model.BookingTableID > 0)
                    {
                        <div class="col-md-12 col-12">
                            <!-- form Start  -->
                            @using (Html.BeginForm("BookingTables", "TableReservation", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
                            {
                                @Html.AntiForgeryToken()
                                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                                @Html.HiddenFor(model => model.BookingTableID)
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Full Name :
                                                @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control", @placeholder = "name" } })
                                                @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Mobile No :
                                                @Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "Contact No" } })
                                                @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Reservation Date :
                                                @Html.EditorFor(model => model.ReservationDate, new { htmlAttributes = new { @class = "form-control", @placeholder = "Reservation Date" } })
                                                @Html.ValidationMessageFor(model => model.ReservationDate, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Reservation Time
                                                @Html.EditorFor(model => model.ReservationTime, new { htmlAttributes = new { @class = "form-control", @placeholder = "Reservation Time" } })
                                                @Html.ValidationMessageFor(model => model.ReservationTime, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Enter Message :
                                                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Message Here" } })
                                                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-12 col-sm-12 col-12">
                                                Process To :
                                                @Html.DropDownList("BookingStatusID", null, "--Choose Type--", htmlAttributes: new { @class = "form-control" })
                                                @Html.ValidationMessageFor(model => model.BookingStatusID, "", new { @class = "text-danger" })
                                            </div>
                                        </div>

                                        <div class="buttons">
                                            <input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Process" />
                                        </div>
                                    </div>
                                </div>
                            }
                            <!-- form End  -->
                        </div>
                    }
                }
                <div class="col-md-12 col-12">
                    <!--  List Start  -->
                    <div class="table-responsive-md">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center">
                                        Status
                                    </th>
                                    <th class="text-center">
                                        Full Name
                                    </th>
                                    <th class="text-center">
                                        Mobile No
                                    </th>
                                    <th class="text-center">
                                        Booking Date
                                    </th>
                                    <th class="text-center">
                                        Reservation Date Time
                                    </th>
                                    <th class="text-center">
                                        No Of Persons
                                    </th>
                                    <th class="text-center">
                                        Description
                                    </th>
                                    <th class="text-center">
                                        Process By
                                    </th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.ReservationList)
                                {
                                    <tr>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.BookingStatus)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.FullName) (@Html.DisplayFor(modelItem => item.BookingUserName))
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.MobileNo)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.BookingDate)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.ReservationDateTime)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.NoOfPersons)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Description)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.ProcessBy_User)
                                        </td>
                                        <td>
                                            @if (usertypeid == 1 || usertypeid == 2 || usertypeid == 3)
                                            {
                                                @Html.ActionLink("Process", "BookingTables", new { id = item.BookingTableID }, new { @class = "btn btn-theme btn-md btn-wide" })
                                            }
                                            else
                                            {
                                                if (string.IsNullOrEmpty(item.ProcessBy_User))
                                                {
                                                @Html.ActionLink("Cancel", "CancelBooking", new { bookingtableid = item.BookingTableID }, new { @class = "btn btn-theme btn-md btn-wide" })
                                                }
                                            }
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                    <!--  List End  -->
                </div>
            }
        </div>
    </div>
</div>
<!--  Reservation End -->

>  CancelBooking Action Code : Add to TableReservationController

 public ActionResult CancelBooking(int bookingtableid)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
            {
                return RedirectToAction("Index", "Home");
            }
            int userid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            if (ModelState.IsValid)
            {
                var reservation = Db.BookingTblTables.Find(bookingtableid);
                reservation.Description = "Canceled By User";
                reservation.BookingStatusID = 4;
                reservation.ProcessBy_UserID = userid;
                Db.Entry(reservation).State = System.Data.Entity.EntityState.Modified;
                Db.SaveChanges();
                return RedirectToAction("BookingTables", new { id = 0 });
            }
            return View();
        }


so once all above steps is done then run the application... 

Comments