Online Restaurant Website Part 6 Basic Tables
Online Restaurant Website Part 6 Basic Tables
Hi, Dear's here we learn how to implement Restaurant 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 basic table of database in asp.net mvc ui project for more details click here:
watch video
Next add empty HomeController in controller folder then add the below code in Index Action.
public ActionResult Index()
{
return RedirectToAction("List_UserTypes","Setting",new { id = 0});
}
> GenderMV Code :
using System.ComponentModel.DataAnnotations;
namespace PizzaRestaurantDrink.Models
{
public class GenderMV
{
[Display(Name ="#Unique No")]
public int GenderID { get; set; }
[Display(Name="Gender")]
public string GenderTitle { get; set; }
}
}
> CRU_GenderMV Code :
using DBLayer;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace PizzaRestaurantDrink.Models
{
public class CRU_GenderMV
{
// Call this Consturctor To Add New Entry
public CRU_GenderMV()
{
GetAllGenders();
}
// Call this Consturctor To Edit Entry
public CRU_GenderMV(int? id)
{
GetAllGenders();
var editgender = new PizzaRestaurentandDrinksDbEntities().GenderTables.Where(g=>g.GenderID == id).FirstOrDefault();
if (editgender != null)
{
GenderID = editgender.GenderID;
GenderTitle = editgender.GenderTitle;
}
else
{
GenderID = 0;
GenderTitle = string.Empty;
}
}
public int GenderID { get; set; }
[Display(Name="Gender")]
[Required(ErrorMessage = "Enter Gender")]
public string GenderTitle { get; set; }
public List<GenderMV> List_Genders { get; set; }
private void GetAllGenders()
{
List_Genders = new List<GenderMV>();
foreach (var gender in new PizzaRestaurentandDrinksDbEntities().GenderTables.ToList())
{
List_Genders.Add(new GenderMV()
{
GenderID = gender.GenderID,
GenderTitle = gender.GenderTitle
});
}
}
}
}
So Now Past below code in Setting Controller
> List_Genders Action Code :
add below code.
using DBLayer;
using PizzaRestaurantDrink.Models;
using System.Linq;
using System.Web.Mvc;
namespace PizzaRestaurantDrink.Controllers
{
public class SettingController : Controller
{
PizzaRestaurentandDrinksDbEntities Db = new PizzaRestaurentandDrinksDbEntities();
// Gender > ADD, EDIT
public ActionResult List_Genders(int? id)
{
var gender = new CRU_GenderMV(id);
return View(gender);
}
[HttpPost]
public ActionResult List_Genders(CRU_GenderMV cRU_GenderMV)
{
if (ModelState.IsValid)
{
if (cRU_GenderMV.GenderID == 0)
{
var checkexist = Db.GenderTables.Where(g => g.GenderTitle == cRU_GenderMV.GenderTitle).FirstOrDefault();
if (checkexist == null)
{
var gender = new GenderTable();
gender.GenderTitle = cRU_GenderMV.GenderTitle;
Db.GenderTables.Add(gender);
Db.SaveChanges();
return RedirectToAction("List_Genders", new { id = 0 });
}
else
{
ModelState.AddModelError("GenderTitle", "All Ready Exist!");
}
}
else
{
var checkexist = Db.GenderTables.Where(g => g.GenderTitle == cRU_GenderMV.GenderTitle && g.GenderID != cRU_GenderMV.GenderID).FirstOrDefault();
if (checkexist == null)
{
var editgender = Db.GenderTables.Find(cRU_GenderMV.GenderID);
editgender.GenderTitle = cRU_GenderMV.GenderTitle;
Db.Entry(editgender).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
return RedirectToAction("List_Genders", new { id = 0 });
}
else
{
ModelState.AddModelError("GenderTitle", "All Ready Exist!");
}
}
}
return View(cRU_GenderMV);
}
}
}
> List_Genders View Code :
@model PizzaRestaurantDrink.Models.CRU_GenderMV
@{
ViewBag.Title = "Genders";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
<div class="container">
<div class="matter">
<h2>Genders</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("~/Setting/List_Genders")">Genders</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>Genders</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 -->
<div class="col-md-5 col-12">
<!-- user type form Start -->
@using (Html.BeginForm("List_Genders", "Setting", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.GenderID)
<div class="form-group row">
<div class="col-md-12 col-sm-12 col-12">
<i class="icofont icofont-ui-user"></i>
@Html.EditorFor(model => model.GenderTitle, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Gender" } })
@Html.ValidationMessageFor(model => model.GenderTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="buttons">
@if (Model.GenderID == 0)
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Create" />
}
else
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Update" />
}
</div>
}
<!-- user type form End -->
</div>
<div class="col-md-7 col-12">
<!-- List Start -->
<div class="table-responsive-md">
<table class="table table-bordered">
<thead>
<tr>
<td class="text-center">
@Html.DisplayNameFor(model => model.GenderTitle)
</td>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.List_Genders)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.GenderTitle)
</td>
<td>
@Html.ActionLink("Edit", "List_Genders", new { id = item.GenderID }, new { @class = "btn btn-theme btn-md btn-wide" })
</td>
</tr>
}
</tbody>
</table>
</div>
<!-- List End -->
</div>
</div>
</div>
</div>
Next we are going to implement UserStatus Table
using System.ComponentModel.DataAnnotations;
namespace PizzaRestaurantDrink.Models
{
public class UserStatusMV
{
[Display(Name = "#Unique No")]
public int UserStatusID { get; set; }
[Display(Name ="Account Status")]
public string UserStatus { get; set; }
}
}
> CRU_UserStatusMV Code :
using DBLayer;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace PizzaRestaurantDrink.Models
{
public class CRU_UserStatusMV
{
PizzaRestaurentandDrinksDbEntities db = new PizzaRestaurentandDrinksDbEntities();
public CRU_UserStatusMV()
{
GetUserStatus();
}
public CRU_UserStatusMV(int? id)
{
GetUserStatus();
var editrecord = db.UserStatusTables.Find(id);
if (editrecord != null)
{
UserStatusID = editrecord.UserStatusID;
UserStatus = editrecord.UserStatus;
}
else
{
UserStatusID = 0;
UserStatus = string.Empty;
}
}
[Display(Name = "#Unique No")]
public int UserStatusID { get; set; }
[Display(Name = "Account Status")]
[Required(ErrorMessage = "Field Required*")]
public string UserStatus { get; set; }
public virtual List<UserStatusMV> List_UserStatuses { get; set; }
private void GetUserStatus()
{
List_UserStatuses = new List<UserStatusMV>();
foreach (var userstatus in db.UserStatusTables.ToList())
{
List_UserStatuses.Add(new UserStatusMV()
{
UserStatusID = userstatus.UserStatusID,
UserStatus = userstatus.UserStatus
});
}
}
}
}
So Now Past below code in Setting Controller
> List_UserStatus Action Code :
add below code.
// User Status> ADD , EDIT
public ActionResult List_UserStatus(int? id)
{
var userstatus = new CRU_UserStatusMV(id);
return View(userstatus);
}
[HttpPost]
public ActionResult List_UserStatus(CRU_UserStatusMV cRU_UserStatusMV)
{
if (ModelState.IsValid)
{
if (cRU_UserStatusMV.UserStatusID == 0)
{
var checkexist = Db.UserStatusTables.Where(s => s.UserStatus.ToUpper() == cRU_UserStatusMV.UserStatus.ToUpper()).FirstOrDefault();
if (checkexist == null)
{
var userstatus = new UserStatusTable();
userstatus.UserStatus = cRU_UserStatusMV.UserStatus;
Db.UserStatusTables.Add(userstatus);
Db.SaveChanges();
return RedirectToAction("List_UserStatus", new { id = 0 });
}
else
{
ModelState.AddModelError("UserStatus", "Field Required*");
}
}
else
{
var checkexist = Db.UserStatusTables.Where(s => s.UserStatus.ToUpper() == cRU_UserStatusMV.UserStatus.ToUpper() && s.UserStatusID != cRU_UserStatusMV.UserStatusID).FirstOrDefault();
if (checkexist == null)
{
var userstatus = Db.UserStatusTables.Find(cRU_UserStatusMV.UserStatusID);
userstatus.UserStatus = cRU_UserStatusMV.UserStatus;
Db.Entry(userstatus).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
return RedirectToAction("List_UserStatus", new { id = 0 });
}
else
{
ModelState.AddModelError("UserStatus", "Field Required*");
}
}
}
else
{
ModelState.AddModelError(string.Empty, "Fill All Field Properly.");
}
return View(cRU_UserStatusMV);
}
> List_UserStatus View Code :
@model PizzaRestaurantDrink.Models.CRU_UserStatusMV
@{
ViewBag.Title = "Account Status";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
<div class="container">
<div class="matter">
<h2>Account Status</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("~/Setting/List_UserStatus")">Account Status</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>Account Status</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 -->
<div class="col-md-5 col-12">
<!-- user type form Start -->
@using (Html.BeginForm("List_UserStatus", "Setting", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserStatusID)
<div class="form-group row">
<div class="col-md-12 col-sm-12 col-12">
<i class="icofont icofont-ui-user"></i>
@Html.EditorFor(model => model.UserStatus, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Account Status" } })
@Html.ValidationMessageFor(model => model.UserStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="buttons">
@if (Model.UserStatusID == 0)
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Create" />
}
else
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Update" />
}
</div>
}
<!-- user type form End -->
</div>
<div class="col-md-7 col-12">
<!-- List Start -->
<div class="table-responsive-md">
<table class="table table-bordered">
<thead>
<tr>
<td class="text-center">
@Html.DisplayNameFor(model => model.UserStatusID)
</td>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.List_UserStatuses)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserStatus)
</td>
<td>
@Html.ActionLink("Edit", "List_UserStatus", new { id = item.UserStatusID }, new { @class = "btn btn-theme btn-md btn-wide" })
</td>
</tr>
}
</tbody>
</table>
</div>
<!-- List End -->
</div>
</div>
</div>
</div>
Next we are going to implement VisibleStatusTable
using System.ComponentModel.DataAnnotations;
namespace PizzaRestaurantDrink.Models
{
public class VisibleStatusMV
{
[Display(Name = "#Unique No")]
public int VisibleStatusID { get; set; }
[Display(Name="Visible Status")]
public string VisibleStatus { get; set; }
}
}
> CRU_VisibleStatusMV Code :
using DBLayer;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace PizzaRestaurantDrink.Models
{
public class CRU_VisibleStatusMV
{
PizzaRestaurentandDrinksDbEntities db = new PizzaRestaurentandDrinksDbEntities();
public CRU_VisibleStatusMV()
{
GetVisbleStatus();
}
public CRU_VisibleStatusMV(int? id)
{
GetVisbleStatus();
var editrecord = db.VisibleStatusTables.Find(id);
if (editrecord != null)
{
VisibleStatusID = editrecord.VisibleStatusID;
VisibleStatus = editrecord.VisibleStatus;
}
else
{
VisibleStatusID = 0;
VisibleStatus = string.Empty;
}
}
[Display(Name = "#Unique No")]
public int VisibleStatusID { get; set; }
[Display(Name = "Visible Status")]
[Required(ErrorMessage = "Field Required*")]
public string VisibleStatus { get; set; }
public virtual List<VisibleStatusMV> List_VisibleStatus { get; set; }
private void GetVisbleStatus()
{
List_VisibleStatus = new List<VisibleStatusMV>();
foreach (var status in db.VisibleStatusTables.ToList())
{
List_VisibleStatus.Add(new VisibleStatusMV()
{
VisibleStatusID = status.VisibleStatusID,
VisibleStatus = status.VisibleStatus
});
}
}
}
}
So Now Past below code in Setting Controller
> List_VisibleStatus Action Code :
add below code.
// User Status> ADD , EDIT
public ActionResult List_VisibleStatus(int? id)
{
var status = new CRU_VisibleStatusMV(id);
return View(status);
}
[HttpPost]
public ActionResult List_VisibleStatus(CRU_VisibleStatusMV cRU_VisibleStatusMV)
{
if (ModelState.IsValid)
{
if (cRU_VisibleStatusMV.VisibleStatusID == 0)
{
var checkexist = Db.VisibleStatusTables.Where(s => s.VisibleStatus.ToUpper() == cRU_VisibleStatusMV.VisibleStatus.ToUpper()).FirstOrDefault();
if (checkexist == null)
{
var status = new VisibleStatusTable();
status.VisibleStatus = cRU_VisibleStatusMV.VisibleStatus;
Db.VisibleStatusTables.Add(status);
Db.SaveChanges();
return RedirectToAction("List_VisibleStatus", new { id = 0 });
}
else
{
ModelState.AddModelError("VisibleStatus", "Field Required*");
}
}
else
{
var checkexist = Db.VisibleStatusTables.Where(s => s.VisibleStatus.ToUpper() == cRU_VisibleStatusMV.VisibleStatus.ToUpper() && s.VisibleStatusID != cRU_VisibleStatusMV.VisibleStatusID).FirstOrDefault();
if (checkexist == null)
{
var editstatus = Db.VisibleStatusTables.Find(cRU_VisibleStatusMV.VisibleStatusID);
editstatus.VisibleStatus = cRU_VisibleStatusMV.VisibleStatus;
Db.Entry(editstatus).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
return RedirectToAction("List_VisibleStatus", new { id = 0 });
}
else
{
ModelState.AddModelError("VisibleStatus", "Field Required*");
}
}
}
else
{
ModelState.AddModelError(string.Empty, "Fill All Field Properly.");
}
return View(cRU_VisibleStatusMV);
}
> List_VisibleStatus View Code :
@model PizzaRestaurantDrink.Models.CRU_VisibleStatusMV
@{
ViewBag.Title = "Visible Status";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
<div class="container">
<div class="matter">
<h2>Record Visible Status</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("~/Setting/List_VisibleStatus")">Record Visible Status</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>Record Visible Status</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 -->
<div class="col-md-5 col-12">
<!-- user type form Start -->
@using (Html.BeginForm("List_VisibleStatus", "Setting", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.VisibleStatusID)
<div class="form-group row">
<div class="col-md-12 col-sm-12 col-12">
<i class="icofont icofont-ui-user"></i>
@Html.EditorFor(model => model.VisibleStatus, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Record Visible Status Title" } })
@Html.ValidationMessageFor(model => model.VisibleStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="buttons">
@if (Model.VisibleStatusID == 0)
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Create" />
}
else
{
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Update" />
}
</div>
}
<!-- user type form End -->
</div>
<div class="col-md-7 col-12">
<!-- List Start -->
<div class="table-responsive-md">
<table class="table table-bordered">
<thead>
<tr>
<td class="text-center">
@Html.DisplayNameFor(model => model.VisibleStatus)
</td>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.List_VisibleStatus)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.VisibleStatus)
</td>
<td>
@Html.ActionLink("Edit", "List_VisibleStatus", new { id = item.VisibleStatusID }, new { @class = "btn btn-theme btn-md btn-wide" })
</td>
</tr>
}
</tbody>
</table>
</div>
<!-- List End -->
</div>
</div>
</div>
</div>
Comments
Post a Comment