Database (EntityFrameworkCore)
Dependencies -> Manage Nuget Packages
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore
Models/AppDbContext.cs
- public class AppDbContext : DbContext
- public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
- public DbSet<Car> Cars {get; set; }
- public DbSet<Category> Categories {get; set;}
appsettings.json
- "ConnectionStrings" :
- "DefaultConnection":
- "Server=(localdb)\mssqllocaldb;Database=CarShopDb;Trusted_Connection=True;MultipleActiveResultSets=true"
Startup.cs
- public IConfiguration Configuration {get;}
- public Startup(IConfiguration configuration)
- services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConneciton")))
Models/CarRepository.cs
- private readonly AppDbContext _appDbContext
- public CarRepository(AppDbContext appDbContext)
Models/CategoryRepository.cs
- private readonly AppDbContext _appDbContext
- public CategoryRepository(AppDbContext appDbContext)
Tools -> Nuget Package Manager -> Package Manager Console
- Install-Package Microsoft.EntityFrameworkCore.Tools
- Update-Package Microsoft.EntityFrameworkCore.Tools
- Add-Migration -Name InitialMigration
- Update-Database -Migration InitialMigration
Models/AddDbContext.cs
- OnModelCreating(ModelBuilder modelBuilder)
Tools -> Nuget Package Manager -> Package Manager Console
- Add-Migration -Name SeedingDb
- Update-Database -Migration SeedingDb
Navigation, routing, tag helpers
Startup.cs
- This is just a trial --- {cotroller=Car}/{action=List}
Controllers/CarController.cs
- public IActionResult Details(int id)
Views/Car/Details.cshtml
- Detail view
@model Car
<h1>@Model.Name</h1>
<div class="thumbnail">
<img src="@Model.ImageUrl" class="img-fluid" alt="@Model.Name" />
<div class="caption-full">
<h3 class="float-right">@Model.Price.ToString("c")</h3>
<h3><a href="#">@Model.Name</a></h3>
<p>@Model.Description</p>
</div>
</div>
Views/_ViewImports.cshtml
- @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Views/Car/List.cshtml
- <h3><a asp-controller="Car" asp-action="Details" asp-route-id="@car.CarId">@car.Name</a></h3>
Run (TagHelper)
Views/Shared/_Layout.cshtml
- <nav>
- <button>
- <li>
Run (Navigation Bar)
Views/Shared/_carCard.cshtml
@model Car
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="caption">
<img src="@Model.ImageThumbNailUrl" alt="Image for eshopCar" />
<h3>@Model.Price.ToString("c")</h3>
<h3><a asp-controller="Car" asp-action="Details" asp-route-id="@Model.CarId">@Model.Name</a></h3>
<p>@Model.Description</p>
</div>
</div>
Views/Car/List.cshtml
@model CarListViewModel
<div class="container">
<div class="row">
<h1>@Model.CurrentCategory</h1>
</div>
<div class="row">
@foreach (var car in Model.Car)
{
<partial name="_carCard" model="car" />
}
</div>
</div>
Run (nothing has changed)
Controllers/HomeController.cs
- var homeViewModel = new HomeViewModel
- CarOnSale = _carRepository.GetCarOnSale
ViewModels/HomeViewModel.cs
- IEnumerable<Car> CarOnSale
View/Home/Index.cshtml
@model HomeViewModel
<div class="container">
<div class="row">
<partial name="_Carousel" />
</div>
<div class="row">
<h2>Car on sale</h2>
</div>
<div class="row">
@foreach (var car in Model.CarOnSale)
{
<partial name="_carCard" model="car" />
}
</div>
</div>
View/Shared/_Carousel.cshtml
<div class="col-md-12">
<div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
<li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="slide-image" src="~/Images/carousel1.jpg" alt="..." />
<div class="carousel-caption d-none d-md-block">
<h5>First slide label</h5>
<p>First slide description</p>
</div>
</div>
<div class="carousel-item">
<img class="slide-image" src="~/Images/carousel2.jpg" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Second slide label</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="carousel-item">
<img class="slide-image" src="~/Images/carousel3.png" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Third slide label</h5>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
Sessions
Models/ShoppingCartItem.cs
- Add properties
Models/AppDbContext.cs
- DbSet<ShoppingCartItem> ShoppingCartItems
Package Manager Console
- Add-Migration -Name AddingShoppingCartItem
- Update-Database -Migration AddingShoppingCartItem
Models/ShoppingCart.cs
- ShoppingCartId
- List<ShoppingCartItem> ShoppingCartItems
- public static ShoppingCart GetCart(IServiceProvider services)
- public void AddToCart()
- public int RemoveFromCart()
- public List<ShoppingCartItem> GetShoppingCartItems()
- public void ClearCart()
- public decimal GetShoppingCartTotal()
Startup.cs
- services.AddScoped<ShoppingCart>(sc => ShoppingCart.GetCart(sc))
- services.AddHttpContextAccessor()
- services.AddSession()
- app.UseSession()
Controllers/ShoppingCartController.cs
- public ViewResult Index()
- public RedirectToActionResult AddToShoppingCart(int carId)
- public RedirectToActionResult RemoveFromShoppingCart(int carId)
ViewModels/ShoppingCartViewModel.cs
- ShoppingCart ShoppingCart
- decimal ShoppingCartTotal
Views/ShoppingCart/Index.cshtml
- Selected amount, Car, Price, Subtotal
- Amount, Name, Price, Amount*Price
- Total
Views/Shared/_carCard.cshtml
- <p class="button">
- <a class="btn btn-primary" asp-controller="ShoppingCart" asp-action="AddToShoppingCart" asp-route-carId="@model.CarId">Add To Cart</a>
Views/Car/Details.cshtml
- Copy button
No comments:
Post a Comment