Online Restaurant Website Part 18 Stock Item Ingredients
Online Restaurant Website Part 18 Stock Item Ingredients
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 Stock Item Ingredient Designing + Implementation for more details click here:
watch vedio First we are going to create StockItemIngredientMV for data model to show data in view. code add below:
> StockItemIngredientMV Model Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PizzaRestaurantDrink.Models
{
public class StockItemIngredientMV
{
public int StockItemIngredientID { get; set; }
public int StockItemID { get; set; }
public string StockItem { get; set; }
public string StockItemIngredientPhotoPath { get; set; }
public string StockItemIngredientTitle { get; set; }
public string CreatedBy { get; set; }
}
}
> CRU_StockItemIngredientMV Model Code : // To Create and get to list of select item ingredient.
using Dblayer;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace PizzaRestaurantDrink.Models
{
public class CRU_StockItemIngredientMV
{
PizzaRestaurentandDrinksDbEntities db = new PizzaRestaurentandDrinksDbEntities();
public CRU_StockItemIngredientMV(){}
public CRU_StockItemIngredientMV(int? id)
{
StockItemID = id != null ? (int)id : 0;
GetIngredients();
}
public int StockItemIngredientID { get; set; }
public int StockItemID { get; set; }
public string StockItemIngredientPhotoPath { get; set; }
public string StockItemIngredientTitle { get; set; }
[NotMapped]
[Display(Name = "Ingredient Photo")]
public HttpPostedFileBase PhotoPath { get; set; }
public virtual List<StockItemIngredientMV> Lists { get; set; }
private void GetIngredients()
{
Lists = new List<StockItemIngredientMV>();
var ingredients = db.StockItemIngredientTables.Where(i => i.StockItemID == StockItemID).ToList();
foreach (var ingredient in ingredients)
{
var itemname = ingredient.StockItemTable.StockItemTitle;
var createdby = db.UserTables.Find(ingredient.CreatedBy_UserID).UserName;
Lists.Add(new StockItemIngredientMV()
{
StockItemIngredientID = ingredient.StockItemIngredientID,
StockItemID = ingredient.StockItemID,
StockItem = itemname,
StockItemIngredientPhotoPath = ingredient.StockItemIngredientPhotoPath,
StockItemIngredientTitle = ingredient.StockItemIngredientTitle,
CreatedBy = createdby
});
}
}
}
}
Next we are going to create action in stock controller to create item ingredients action name 'StockItemIngredient', code show below
Note : create folder in content folder to save item ingredient's photo's
> StockItemIngredient Action Code :
public ActionResult StockItemIngredient(int? id)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
{
return RedirectToAction("Index", "Home");
}
var list = new CRU_StockItemIngredientMV(id);
return View(list);
}
[HttpPost]
public ActionResult StockItemIngredient(CRU_StockItemIngredientMV cRU_StockItemIngredientMV)
{
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)
{
if (cRU_StockItemIngredientMV.StockItemID > 0)
{
var checkexist = Db.StockItemIngredientTables.Where(
s => s.StockItemIngredientTitle.Trim().ToUpper() == cRU_StockItemIngredientMV.StockItemIngredientTitle.Trim().ToUpper()
&& s.StockItemID == cRU_StockItemIngredientMV.StockItemID).FirstOrDefault();
if (checkexist == null)
{
var newingredient = new StockItemIngredientTable();
newingredient.StockItemID = cRU_StockItemIngredientMV.StockItemID;
newingredient.StockItemIngredientPhotoPath = @"~/Content/StockIngredientPhoto/default.png";
newingredient.StockItemIngredientTitle = cRU_StockItemIngredientMV.StockItemIngredientTitle;
newingredient.CreatedBy_UserID = userid;
Db.StockItemIngredientTables.Add(newingredient);
Db.SaveChanges();
if (cRU_StockItemIngredientMV.PhotoPath != null)
{
var folder = "~/Content/StockIngredientPhoto";
var photoname = string.Format("{0}.jpg", newingredient.StockItemIngredientID);
var response = HelperClass.FileUpload.UploadPhoto(cRU_StockItemIngredientMV.PhotoPath, folder, photoname);
if (response)
{
var photo = string.Format("{0}/{1}", folder, photoname);
newingredient.StockItemIngredientPhotoPath = photo;
Db.Entry(newingredient).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
}
}
return RedirectToAction("StockItemIngredient", new { id = cRU_StockItemIngredientMV.StockItemID });
}
else
{
ModelState.AddModelError("StockItemIngredientTitle", "Already Exist!");
}
}
else
{
ModelState.AddModelError(string.Empty, "Close and Select Item First!");
}
}
var list = new CRU_StockItemIngredientMV(cRU_StockItemIngredientMV.StockItemID);
list.StockItemIngredientTitle = cRU_StockItemIngredientMV.StockItemIngredientTitle;
list.StockItemID = cRU_StockItemIngredientMV.StockItemID;
list.PhotoPath = cRU_StockItemIngredientMV.PhotoPath;
return View(list);
}
> StockItemIngredient View Code :
@model PizzaRestaurantDrink.Models.CRU_StockItemIngredientMV
@{
ViewBag.Title = "Stock";
}
<!-- Breadcrumb Start -->
<div class="bread-crumb">
<div class="container">
<div class="matter">
<h2>Stock Item</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("~/Stock/StockItem")">Stock Items</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>Stock Item Ingredients</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-12 col-12">
<!-- user type form Start -->
@using (Html.BeginForm("StockItemIngredient", "Stock", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StockItemIngredientID)
@Html.HiddenFor(model => model.StockItemID)
<div class="form-group row">
<div class="col-md-12 col-sm-12 col-12">
@Html.EditorFor(model => model.StockItemIngredientTitle, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Intgredient Title" } })
@Html.ValidationMessageFor(model => model.StockItemIngredientTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-md-12 col-sm-12 col-12">
<label style="margin-left:20px;">Upload Ingredient Photo</label>
<br />
<span class="btn btn-default btn-file">
@Html.TextBoxFor(model => model.PhotoPath, new { @class = "upload-pic form-control-file", @type = "file" })
</span>
</div>
</div>
<div class="buttons">
<input class="btn btn-theme btn-md btn-wide" style="float:right;" type="submit" value="Create" />
</div>
}
<!-- user type form End -->
</div>
<div class="col-md-12 col-12">
<!-- List Start -->
<div class="table-responsive-md">
<table class="table table-bordered">
<thead>
<tr>
<td class="text-center">
Image
</td>
<td class="text-center">
Item Title
</td>
<td class="text-center">
Ingredient Title
</td>
<td class="text-center">
Created By
</td>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Lists)
{
<tr>
<td>
<a href="@Url.Content(item.StockItemIngredientPhotoPath)">
<img class="mr-3" src="@Url.Content(item.StockItemIngredientPhotoPath)" style="width:100px;height:150px;max-width: 100%; height: auto;" alt="Image">
</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.StockItem)
</td>
<td>
@Html.DisplayFor(modelItem => item.StockItemIngredientTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedBy)
</td>
<td>
@Html.ActionLink("Delete", "DeleteIngredient", new { id = item.StockItemIngredientID }, 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 create action to delete item ingredients action name 'DeleteIngredient', code show below
> DeleteIngredient Action Code :
public ActionResult DeleteIngredient(int? id)
{
var ingredient = Db.StockItemIngredientTables.Find(id);
Db.Entry(ingredient).State = System.Data.Entity.EntityState.Deleted;
Db.SaveChanges();
return RedirectToAction("StockItemIngredient", new { id = ingredient.StockItemID });
}
Comments
Post a Comment