Online Restaurant Website Part 40 Place Order

     Online Restaurant Website Part 40 Place Order 

Hi, Dear's here we learn how to implement "Pizza Restaurant Drink" 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 finish place order for more details click here: watch vedio

First we are going to open project , then go to order controller:

and open CartView and set post method. show in code below

> ViewCart Update Code: 

@using(Html.BeginForm("ViewCart", "Order", FormMethod.Post, new { @enctype = "multipart/form-data", @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
....
}

Create ViewCart Post Method to post shopping cart full details to order tables. 

> ViewCart Post Method: 

 [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewCart(ShoppingCartMV obj)
{
    if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
    {
        return RedirectToAction("Login", "User");
    }
    int userid = 0;
    int.TryParse(Convert.ToString(Session["UserID"]), out userid);
}

Create sql transection in linq query to perform place order procedure safely step by step means during error or issue don't post non-required data.

 using (var transaction = Db.Database.BeginTransaction())
 {
     try
     { 
        transaction.Rollback();
        ModelState.AddModelError("", "Some Issue is Occure! Please Re-login and Try Again.");
    }
    catch (Exception ex)
    {
        transaction.Rollback();
        ModelState.AddModelError("", "Some Issue is Occure! Please Re-login and Try Again.");
    }

}

Now retrieve shopping details from cart tables.
and add to order tables . 
 var cart = Db.CartTables.Where(u => u.UserID == userid).FirstOrDefault();
 int cartid = (cart != null ? cart.CartID : 0);
 if (cartid > 0)
 {
     var order_Header = new OrderTable();
     order_Header.OrderBy_UserID = userid;
     order_Header.OrderDateTime = DateTime.Now;
     order_Header.OrderTypeID = obj.OrderTypeID;
     order_Header.DeliveryAddress_UserAddressID = obj.UserAddressID;
     order_Header.ProcessBy_UserID = userid;
     order_Header.OrderReceivedBy_ContactNo = obj.ContactNo;
     order_Header.OrderReceivedBy_FullName = obj.FirstName + " " + obj.LastName;
     order_Header.OrderStatusID = 1; // Pending 
     order_Header.Description = "Please Wait...";
     order_Header.OrderPaymentID = 1;
     Db.OrderTables.Add(order_Header);
     Db.SaveChanges();

     var cartItems = Db.CartItemDetailTables.Where(i => i.CartID == cartid);
     foreach (var cart_item in cartItems.ToList())
     {
         var item = new OrderItemDetailTable()
         {
             OrderID = order_Header.OrderID,
             StockItemID = cart_item.StockItemID,
             UnitPrice = cart_item.StockItemTable.UnitPrice,
             Qty = cart_item.Qty,
             DiscountID = 1,
             DiscountAmount = 0
         };
         Db.OrderItemDetailTables.Add(item);
         Db.SaveChanges();
     }
     Db.CartItemDetailTables.RemoveRange(cartItems);
     Db.SaveChanges();

     var deals = Db.CartDealTables.Where(d => d.CartID == cartid);
     foreach (var cart_deal in deals.ToList())
     {
         var deal = new OrderDealDetailTable()
         {
             OrderID = order_Header.OrderID,
             StockDealID = cart_deal.StockDealID,
             DealPrice = cart_deal.StockDealTable.DealPrice,
             Qty = cart_deal.Qty,
         };
         Db.OrderDealDetailTables.Add(deal);
         Db.SaveChanges();
         Db.Entry(cart_deal).State = System.Data.Entity.EntityState.Deleted;
         Db.SaveChanges();
     }

     Db.CartDealTables.RemoveRange(deals);
     Db.SaveChanges();

     Db.Entry(cart).State = System.Data.Entity.EntityState.Deleted;
     Db.SaveChanges();

     transaction.Commit();
     return RedirectToAction("Dashboard", "User"); // User/Dashboard
 }


in case of any issue detected during place order in post method, to initailze shopping cart obj to show shopping details in ViewCart view .  

var shoppingcart = Cart(userid);
ViewBag.UserAddressID = new SelectList(Db.UserAddressTables.Where(ua => ua.UserID == userid).ToList(), "UserAddressID", "FullAddress", obj.UserAddressID);
ViewBag.OrderTypeID = new SelectList(Db.OrderTypeTables.ToList(), "OrderTypeID", "OrderType", obj.OrderTypeID);
return View(shoppingcart);
  

once all the above step is done, run the application. 


Comments