@model ShopCartViewModel
<div class="container">
@foreach (var el in Model.shopCart.listShopItems)
{
<div class="alert alert-warning mt-3">
<b>Адресс:</b>@el.realty.adress<br />
<b>Цена:</b>@el.realty.price.ToString("c")
</div>
}
<hr />
<div class="btn btn-danger" asp-controller="Order">Оплатить</div>
</div>
<h2>Вся недвижимость</h2>
<h3>@Model.shopCart</h3>
<div class="row mt-5 mb-4">
@{
foreach (Realty realt in Model.shopCart)
{
@Html.Partial("AllRealty", realt)
}
}
</div>
public class ShopCartController : Controller
{
private readonly IAllRealty _realtyRep;
private readonly ShopCart _shopCart;
public ShopCartController(IAllRealty realRep, ShopCart shopCart)
{
_realtyRep = realRep;
_shopCart = shopCart;
}
public ViewResult Index()
{
var items = _shopCart.GetShopItems();
_shopCart.listShopItems = items;
var obj = new ShopCartViewModel
{
shopCart = _shopCart
};
return View(obj);
}
public RedirectToActionResult addToCart(int id)
{
var item = _realtyRep.Realty.FirstOrDefault(i => i.id == id);
if (item != null)
{
_shopCart.AddToCart(item);
}
return RedirectToAction("Index");
}
}