Online Restaurant Website Part 30 Product Rating
Hi, Dear's here we learn how to implement "Pizza Restaurant Drink" Website in Visual S
tudio 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 Product Review for more details click here:
watch vedioFirst we are going to open database and delete OrderReviewTable and Re-Create on [StockItemReviewTable] to store product reviews and then code add below:
> StockItemReviewTable Table:
USE [Pro_PizzaRestaurentandDrinksDb]
GO
DROP Table OrderReviewTable
CREATE TABLE [dbo].[StockItemReviewTable](
[ItemReviewID] [int] IDENTITY(1,1) NOT NULL,
[StockItemID] [int] NOT NULL,
[ReviewBy_UserID] [int] NOT NULL,
[ReviewDateTime] DateTime NOT NULL,
[Rating] [int] NOT NULL,
[ReviewDetails] [nvarchar](max) NOT NULL
)
Next we are going to update entity data model in database layer.
Next we are going to update entity data model in database layer to add stockitemreviewtable:
Next update ItemDetailMV Model code, show below
ItemDetailMV Model Update Code :
namespace PizzaRestaurantDrink.Models
{
public class ItemDetailMV
{
public ItemDetailMV()
{
.....
RatingList = new List<Rating>
{
new Rating{ID="1" , Type = "Worst"},
new Rating{ID="2" , Type = "Bad"},
new Rating{ID="3" , Type = "Neutral"},
new Rating{ID="4" , Type = "Good"},
new Rating{ID="5" , Type = "Excellent"}
};
}
public string Name { get; set; }
public string Email { get; set; }
public int StockItemID { get; set; }
[Required(ErrorMessage = "Rating Required")]
[Display(Name = "Rating :")]
public string Rating { get; set; }
public List<Rating> RatingList { get; set; }
public string ReviewDetails { get; set; }
.....
}
public class Rating
{
public string ID { get; set; }
public string Type { get; set; }
}
}
Next update ProductDetail(Get Method) Action code in home controller
ProductDetail Action Code :
// Get Method
public ActionResult ProductDetail(int itemid) {
if (itemid == null | itemid == 0)
{
return RedirectToAction("Index","Home");
}
var item_detail = new ItemDetailMV();
item_detail.StockItemID = (int)itemid;
if (!string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
{
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
var userdetail = db.UserTables.Find(userid);
item_detail.Name = userdetail.FirstName + " " + userdetail.LastName;
item_detail.Email = userdetail.EmailAddress;
var review_existing = db.StockItemReviewTables.Where(u => u.ReviewBy_UserID == userid && u.StockItemID == itemid).FirstOrDefault();
if (review_existing != null)
{
item_detail.Rating = Convert.ToString(review_existing.Rating);
item_detail.ReviewDetails = review_existing.ReviewDetails;
}
else
{
item_detail.Rating = "0";
item_detail.ReviewDetails = string.Empty;
}
}
// Getting Item Details
.......
// Getting Customer's Reviews from Item Review New Table
var reviews = db.StockItemReviewTables.Where(i => i.StockItemID == itemid).ToList();
int total_reviews = 0;
int total_rating = 0;
for (int i = 0; i < reviews.Count(); i++)
{
total_reviews = total_reviews + 1;
total_rating = total_rating + reviews[i].Rating;
var review_user = db.UserTables.Find(reviews[i].ReviewBy_UserID).UserName;
var userdetail = db.UserTables.Find(reviews[i].ReviewBy_UserID).UserDetailTable;
var user_photo_path = userdetail != null ? userdetail.PhotoPath : "~/Content/ProfilePhoto/user_default.png";
var review_days = (DateTime.Now - reviews[i].ReviewDateTime).TotalDays;
item_detail.Reviews.Add(new ItemReviewMV()
{
Review_Days = (int)review_days,
Rating = reviews[i].Rating,
ReviewBy_User = review_user,
UserPhotoPath = user_photo_path,
ReviewDetails = reviews[i].ReviewDetails
});
}
int total_reviews_score = total_reviews * 5;
int item_rating = total_reviews_score > 0 ? (total_rating / total_reviews_score) * 5 : 0;
item_detail.Item.Rating = item_rating;
// getting ingredients
return View(item_detail);
}
Next Create ProductDetail(Post Method) Action code in home controller
ProductDetail (Post Method) Action Code :
// Post Method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ProductDetail(ItemDetailMV itemDetailMV)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
{
return RedirectToAction("Index", "Home");
}
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
var review_existing = db.StockItemReviewTables.Where(u => u.ReviewBy_UserID == userid && u.StockItemID == itemDetailMV.StockItemID).FirstOrDefault();
if (review_existing != null)
{
review_existing.ReviewDateTime = DateTime.Now;
review_existing.Rating = Convert.ToInt32(itemDetailMV.Rating);
review_existing.ReviewDetails = itemDetailMV.ReviewDetails;
db.Entry(review_existing).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
else
{
var itemreview = new StockItemReviewTable();
itemreview.StockItemID = itemDetailMV.StockItemID;
itemreview.ReviewBy_UserID = userid;
itemreview.ReviewDateTime = DateTime.Now;
itemreview.Rating = Convert.ToInt32(itemDetailMV.Rating);
itemreview.ReviewDetails = itemDetailMV.ReviewDetails;
db.StockItemReviewTables.Add(itemreview);
db.SaveChanges();
}
return RedirectToAction("ProductDetail", new { itemid = itemDetailMV.StockItemID });
}
Next Update ProductDetail View
ProductDetail View (Update) Code :
// First Add this stylesheet to ProductDetail View Layout.cshtml
<!--Product Star stylesheet-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
// Code for Show Product Rating
<div>
@if (review.Rating == 5)
{
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
}
else if (review.Rating == 4)
{
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star"></span>
}
else if (review.Rating == 3)
{
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
else if (review.Rating == 2)
{
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
else if (review.Rating == 1)
{
<span class="fa fa-star" style="color:orange;"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
else if (review.Rating == 0)
{
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
</div>
====================================================
//Add this code inside customer review form tab
<div class="tab-pane" id="tab-add">
@{
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
if (userid > 0)
{
using (Html.BeginForm("ProductDetail", "Home", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StockItemID)
<div class="form-group row required">
<div class="col-md-6 col-sm-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name*", @type = "text" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
<div class="col-md-6 col-sm-12">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email*", @type = "email" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row required">
<div class="col-sm-12">
@Html.TextAreaFor(m => m.ReviewDetails,
new
{
@placeholder = "Your Reviews*",
@class = "form-control",
rows = "5",
@required = "true"
})
@Html.ValidationMessageFor(model => model.ReviewDetails, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
@*<div class="rating">*@
<p>Your Rating*</p>
<ul class="list-inline">
@{ foreach (var rate in Model.RatingList)
{
<li class="list-inline-item">
@Html.RadioButtonFor(model => model.Rating, rate.ID )
@Html.Label("" + rate.ID, rate.Type)
</li>
}
}
</ul>
@*</div>*@
</div>
</div>
<div class="buttons clearfix">
<button type="submit" id="button-review" class="btn btn-theme btn-wide">Submit</button>
</div>
}
}
else
{
<a href="@Url.Content("~/User/Login")">First Login here</a>
}
}
</div>
so once all above steps is done then run the application...
Comments
Post a Comment