0% found this document useful (0 votes)
234 views11 pages

Tehnologii Web Lab 6

This document summarizes the steps taken to implement a shopping cart functionality for a web application: 1. New Cart and CartLine classes were added to model the shopping cart and its items. 2. A Cart controller was implemented with actions to add/remove items and display the cart contents. 3. A view displays the cart contents, including quantities, items, prices and totals. 4. A partial view was added to render the cart summary on the shared layout, allowing quick access to the cart from all pages.

Uploaded by

ViorelRotari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views11 pages

Tehnologii Web Lab 6

This document summarizes the steps taken to implement a shopping cart functionality for a web application: 1. New Cart and CartLine classes were added to model the shopping cart and its items. 2. A Cart controller was implemented with actions to add/remove items and display the cart contents. 3. A view displays the cart contents, including quantities, items, prices and totals. 4. A partial view was added to render the cart summary on the shared layout, allowing quick access to the cart from all pages.

Uploaded by

ViorelRotari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ministerul Educației al Republicii Moldova

Universitatea Tehnică a Moldovei

Facultatea Calculatoare, Informatică şi Microelectronică

Departamentul Ingineria Software şi Automatică

Raport
Lucrarea de laborator nr.6
Disciplina: Tehnologii WEB

A efectuat: st. gr. Rotari Viorel


A verificat conf.univ. Cristian Rusu

Chișinău – 2019
Sarcina lucrării: Finisarea functionalului paginii Web
I we'll create a shopping cart as you would probably have seen in internet
markets
Listeningul:
Add new class CartLinein project sportsstore.Models

public class CartLine


{
public Product Product { get; set; }
public int Quantity { get; set; }
}

public class Cart


{
private List<CartLine> goods = new List<CartLine>();

public void AddItem(Product product, int quantity)


{
CartLine line = goods.Where(p => p.Product.ProductID ==
product.ProductID).FirstOrDefault();

if (line == null)
{
goods.Add(new CartLine {Product = product, Quantity = quantity});
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
goods.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return goods.Sum(e => e.Product.Price * e.Quantity);
}
public void Clear()
{
goods.Clear();
}
public IEnumerable<CartLine> Lines
{
get { return goods; }
}
}

Edit the Views/Shared/ProductSummary.cshtml

@model SportsStore.Models.Entities.Product
<div class="item">
<h3>@Model.Name</h3>
@Model.Description
@using (Html.BeginForm("AddToCart", "Cart"))
{
@Html.HiddenFor(x => x.ProductID)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
<input type="submit" value="+ Add to cart" />
}

<h4>@Model.Price.ToString("c")</h4>
</div>

Implementing the Cart controller. Add new controller called Cart

public class CartController : Controller


{
private IProductRepository repository;
public CartController(IProductRepository repository)
{
this.repository = repository;
}

public RedirectToRouteResult AddToCart(int productId, string returnUrl)


{
Product product = repository.Products.FirstOrDefault(p => p.ProductID ==
productId);

if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}

public RedirectToRouteResult RemoveFromCart(int productId, string returnUrl)


{
Product product = repository.Products.FirstOrDefault(p => p.ProductID ==
productId);

if (product != null)
{
GetCart().RemoveLine(product);
}
return RedirectToAction("Index", new { returnUrl });
}

private Cart GetCart()


{
Cart cart = (Cart)Session["Cart"]; //Session object is used for keeping session
state

if (cart == null)
{
cart = new Cart();
Session["Cart"] = cart;
}

return cart;
}
}

Displaying the Contents of the Cart. Before it we need a model class


CartIndexViewModel that will be used by Index action.

namespace SportsStore.WebUI.Models
{
public class CartIndexViewModel
{
public Cart Cart { get; set; }
public string ReturnUrl { get; set; }
}
}
Add Index action in Cart controller

public ViewResult Index(string returnUrl)


{
return View(new CartIndexViewModel { Cart = GetCart(), ReturnUrl =
returnUrl });
}

Add the view for Index action, created above


@model SportsStore.WebUI.Models.CartIndexViewModel
@{
ViewBag.Title = "Sports Store: Your Cart";
}
<h2>Your cart</h2>
<table width="60%" align="center">
<thead>
<tr>
<th align="left">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr>
</thead>
<tbody>
@foreach (var line in Model.Cart.Lines)
{
<tr>
<td align="left">@line.Quantity</td>
<td align="left">@line.Product.Name</td>
<td align="right">@line.Product.Price.ToString("c")</td>
<td align="right">@((line.Quantity *
line.Product.Price).ToString("c"))</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="3" align="right">Total:</td>
<td align="right">
@Model.Cart.ComputeTotalValue().ToString("c")
</td>
</tr>
</tfoot>
</table>
<p align="center" class="actionButtons">
<a href="@Model.ReturnUrl">Continue shopping</a>
</p>
In order to see summary the user has to add an item in the card to see
overall information

Add a new action in the Card controller


public PartialViewResult Summary()
{
var cart = GetCart();
return PartialView(cart);
}
Add view for action Summary
@model SportsStore.Models.Entities.Cart
<div id="cart">
<span class="caption">
<b>Your cart:</b>
@Model.Lines.Sum(x => x.Quantity) item(s),
@Model.ComputeTotalValue().ToString("c")
</span>
@Html.ActionLink("Checkout", "Index", "Cart",
new { returnUrl = Request.Url.PathAndQuery }, null)
</div>

Include this partial view in the _Layout.cshtm

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/Site.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="header">
@{Html.RenderAction("Summary", "Cart");} //this piece of code to be
addded
<div class="title">SPORTS STORE</div>
</div>
<div id="categories">
@{ Html.RenderAction("Menu", "Nav"); }
</div>
<div id="content">
@RenderBody()
</div>
</body>
</html>

In Final version
Concluzii:

You might also like