Advertisement
Advertisement

More Related Content

Similar to TechDays 2016 - Developing websites using asp.net core mvc6 and entity framework core(20)

Advertisement
Advertisement

TechDays 2016 - Developing websites using asp.net core mvc6 and entity framework core

  1. • • • C# • • ASP.NET MVC XAML Windows 8 + 10 • • MS SQL Server • • Trainer Developer • www.reflectionit.nl 3
  2. • • • • • • • • 4
  3. • • • • • • • • • https://github.com/aspnet • http://get.asp.net • https://docs.asp.net 7
  4. • 8
  5. 9
  6. • • • 10
  7. • • • • • https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell 12
  8. • • • • •
  9. • 14
  10. 15
  11. public partial class NorthwindContext : DbContext { #if DEBUG // This constructor is only used for scaffolding controllers public NorthwindContext() : base( new DbContextOptionsBuilder<NorthwindContext>() .UseSqlServer("Server=.sqlexpress;Database=Northwind;Trusted_Connection=True;MultipleActiveResultSets=true") .Option​s) { } #endif public NorthwindContext(DbContextOptions<NorthwindContext> options) : base(options) { } 16
  12. • 17
  13. 18
  14. 19
  15. 20 Async actions!
  16. <ul class="nav navbar-nav"> <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li> <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li> <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li> <li><a asp-area="" asp-controller="Products" asp-action="Index">Products</a></li> </ul> 21
  17. 22
  18. @model IEnumerable<ModelsDemo.Models.Northwind.Products> @{ ViewData["Title"] = "Products"; } <h1>Products</h1> <p><a asp-action="Create"><span class="glyphicon glyphicon-plus" aria- hidden="true"></span> Create New</a></p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.ProductName) </th> <th> @Html.DisplayNameFor(model => model.Supplier.CompanyName) </th> <th> @Html.DisplayNameFor(model => model.Category.CategoryName) </th> http://getbootstrap.com/components/#glyphicons 23 <td> <a asp-action="Edit" asp-route-id="@item.ProductId"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </a> | <a asp-action="Details" asp-route-id="@item.ProductId"> <span class="glyphicon glyphicon-open" aria-hidden="true"></span> </a> | <a asp-action="Delete" asp-route-id="@item.ProductId"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </a> </td>
  19. • @model IEnumerable<ModelsDemo.Models.Northwind.Products> @{ ViewData["Title"] = "Products"; } <h1>Products</h1> <form method="post" class="form-inline"> <input name="filter" class="form-control" value="@ViewData["Filter"]" /> <button type="submit" class="btn btn-info btn-sm">Search</button> </form> <br /> <p> <a asp-action="Create"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create New </a> </p> <table class="table"> 24
  20. public class ProductsController : Controller { private readonly NorthwindContext _context; public ProductsController(NorthwindContext context) { _context = context; } // GET: Products public async Task<IActionResult> Index() { var qry = _context.Products.AsNoTracking().Include(p => p.Category).Include(p => p.Supplier); return View(await qry.ToListAsync()); } [HttpPost] public async Task<IActionResult> Index(string filter) { ViewData["Filter"] = filter; var qry = _context.Products.AsNoTracking().Include(p => p.Category).Include(p => p.Supplier) .Where(p => p.ProductName.Contains(filter)); return View(await qry.ToListAsync()); } 25 Add AsNoTracking()
  21. • <form method="get" class="form-inline"> <input name="filter" class="form-control" value="@ViewData["Filter"]" /> <button type="submit" class="btn btn-info btn-sm">Search</button> </form> • // GET: Products public async Task<IActionResult> Index(string filter) { var qry = _context.Products.AsNoTracking() .Include(p => p.Category) .Include(p => p.Supplier) .OrderBy(p => p.ProductName).AsQueryable(); if (!string.IsNullOrEmpty(filter)) { ViewData["Filter"] = filter; qry = qry.Where(p => p.ProductName.StartsWith(filter)); } return View(await qry.ToListAsync()); } 26
  22. • • • https://github.com/sonnemaf/ReflectionIT.Mvc.Paging • • • •
  23. • public class SuppliersController : Controller { ... // GET: Suppliers public async Task<IActionResult> Index(int page = 1) { var qry = _context.Suppliers.OrderBy(p => p.CompanyName); var model = await PagingList<Suppliers>.CreateAsync(qry, 10, page); return View(model); } 28
  24. • • public void ConfigureServices(IServiceCollection services) { // Add framework services. ... services.AddMvc(); services.AddPaging(); • • @await Component.InvokeAsync("ReflectionIT.Mvc.Paging.Pager", new { pagingList = this.Model }) • • @await Html.PartialAsync("Pager", this.Model)
  25. 30
  26. @model ReflectionIT.Mvc.Paging.IPagingList @if (this.Model.PageCount > 1) { <ul class="pagination"> @for (int i = 1; i <= Model.PageCount; i++) { <li class="@((Model.PageIndex == i) ? "active" : null)"> @Html.ActionLink(i.ToString(), Model.Action, Model.GetRouteValueForPage(i)) </li> } </ul> } 31
  27. • // GET: Products public async Task<IActionResult> Index(string filter, int page = 1, string sortExpression = "ProductName") { var qry = _context.Products .Include(p => p.Category) .Include(p => p.Supplier) .AsQueryable(); if (!string.IsNullOrWhiteSpace(filter)) { qry = qry.Where(p => p.ProductName.Contains(filter)); } var model = await PagingList<Products>.CreateAsync(qry, 10, page, sortExpression, "ProductName"); model.RouteValue = new RouteValueDictionary { { "filter", filter} }; return View(model); } 32
  28. @model ReflectionIT.Mvc.Paging.PagingList<WebApplication2.Models.Products> @using ReflectionIT.Mvc.Paging; @*Brings SortableHeaderFor extension method into scope*@ @{ ViewData["Title"] = "Products"; } <h1>Products</h1> <p><a asp-action="Create">Create New</a></p> <form method="get" asp-antiforgery="false"> <div class="form-group"> <label class="sr-only" for="filter">Filter</label> @Html.TextBox("filter", null, null, new { type = "search", @class = "form-control", placeholder = "your filter" }) </div> <div class="form-group"> <button class="btn btn-primary">Search</button> </div> </form> @Html.Partial("Pager", this.Model) <table class="table table-striped"> <tr> <th>@Html.SortableHeaderFor(model => model.ProductName, "ProductName")</th> <th>@Html.SortableHeaderFor(model => model.Category.CategoryName, "Category.CategoryName, ProductName")</th> <th>@Html.SortableHeaderFor(model => model.Supplier.CompanyName, "Supplier.CompanyName, ProductName") </th> <th>@Html.SortableHeaderFor(model => model.QuantityPerUnit, "QuantityPerUnit") </th> 33
  29. • System.ComponentModel.DataAnnotations • Compare • • CustomValidation • • EmailAddress • • Range • • RegularExpression • • Required • • StringLength MaxLength MinLength • • Url • • • Remote • • 35
  30. public partial class Products { public Products() { OrderDetails = new HashSet<OrderDetails>(); } [Key] public int ProductID { get; set; } public int? CategoryID { get; set; } public bool Discontinued { get; set; } [Required] [MaxLength(40)] [MinLength(4)] public string ProductName { get; set; } [Range(1, 1000)] public decimal? UnitPrice { get; set; } [MaxLength(20)] public string QuantityPerUnit { get; set; } ... 36
  31. public class SuppliersController : Controller { public JsonResult IsCompanyNameAvailable(int? supplierId, string companyName) { if (!_context.Suppliers.Any(sup => (!supplierId.HasValue || sup.SupplierId != supplierId) && sup.CompanyName == companyName)) { return Json(true); } return Json($"{companyName} is already used"); } public partial class Suppliers { [Column("SupplierID")] [Key] public int SupplierId { get; set; } [Required] [MaxLength(40)] [Microsoft.AspNetCore.Mvc.Remote("IsCompanyNameAvailable", "Suppliers", "", AdditionalFields = "SupplierId")] public string CompanyName { get; set; }
  32. // POST: Products/Create [HttpPost][ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("ProductId,CategoryId,Discontinued,ProductName,QuantityPerUnit,ReorderLevel,SupplierId,UnitPrice,UnitsInStock,UnitsOnOr der")] Products products) { if (ModelState.IsValid) { _context.Add(products); //await _context.SaveChangesAsync(); if (await TrySaveChangesAsync()) { return RedirectToAction("Index"); } } ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryName", products.CategoryId); ViewData["SupplierId"] = new SelectList(_context.Suppliers, "SupplierId", "CompanyName", products.SupplierId); return View(products); } private async Task<bool> TrySaveChangesAsync() { try { await _context.SaveChangesAsync(); return true; } catch (DbUpdateException ex) when ((ex.InnerException as SqlException).Number == 2601) { this.ModelState.AddModelError(string.Empty, "ProductName must be unique"); return false; } catch (Exception ex) { this.ModelState.AddModelError(string.Empty, ex.Message); return false; } }
  33. • DisplayAttribute • • DisplayFormatAttribute • • HiddenInputAttribute • • UIHint • • DataTypeAttribute • 41
  34. @{ string style = null; if (ViewData.Model < 20) { style = "color: red"; } if (ViewData.Model > 50) { style = "color: green"; } } <span style="@style">€ @(ViewData.Model?.ToString("N2"))</span>
  35. 45
Advertisement