From 60cdf6f98b107c30c3f5de691371724305bb8c23 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 19 May 2019 23:32:06 +0100 Subject: [PATCH] Optimize view of company admin page. --- .../Admin/Controllers/CompaniesController.cs | 30 +++++++++++---- .../Areas/Admin/Models/CompanyViewModel.cs | 37 +++++++++++++++++++ .../Areas/Admin/Views/Companies/Index.cshtml | 10 ++--- cicm_web/cicm_web.csproj | 2 +- 4 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 cicm_web/Areas/Admin/Models/CompanyViewModel.cs diff --git a/cicm_web/Areas/Admin/Controllers/CompaniesController.cs b/cicm_web/Areas/Admin/Controllers/CompaniesController.cs index 53f589bf..5985a15a 100644 --- a/cicm_web/Areas/Admin/Controllers/CompaniesController.cs +++ b/cicm_web/Areas/Admin/Controllers/CompaniesController.cs @@ -31,6 +31,7 @@ using System.Linq; using System.Threading.Tasks; using Cicm.Database.Models; +using cicm_web.Areas.Admin.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; @@ -55,7 +56,16 @@ namespace cicm_web.Areas.Admin.Controllers { IIncludableQueryable cicmContext = _context.Companies.Include(c => c.Country).Include(c => c.SoldTo); - return View(await cicmContext.OrderBy(c => c.Name).ToListAsync()); + return View(cicmContext.OrderBy(c => c.Name).Select(c => new CompanyViewModel + { + Id = c.Id, + Name = c.Name, + Founded = c.Founded, + Status = c.Status, + Country = c.Country.Name, + Sold = c.Sold, + SoldTo = c.SoldTo.Name + })); } // GET: Admin/Companies/Details/5 @@ -95,8 +105,10 @@ namespace cicm_web.Areas.Admin.Controllers return RedirectToAction(nameof(Index)); } - ViewData["CountryId"] = new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); - ViewData["SoldToId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); + ViewData["CountryId"] = + new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); + ViewData["SoldToId"] = + new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); return View(company); } @@ -108,8 +120,10 @@ namespace cicm_web.Areas.Admin.Controllers Company company = await _context.Companies.FindAsync(id); if(company == null) return NotFound(); - ViewData["CountryId"] = new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); - ViewData["SoldToId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); + ViewData["CountryId"] = + new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); + ViewData["SoldToId"] = + new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); return View(company); } @@ -142,8 +156,10 @@ namespace cicm_web.Areas.Admin.Controllers return RedirectToAction(nameof(Index)); } - ViewData["CountryId"] = new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); - ViewData["SoldToId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); + ViewData["CountryId"] = + new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId); + ViewData["SoldToId"] = + new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId); return View(company); } diff --git a/cicm_web/Areas/Admin/Models/CompanyViewModel.cs b/cicm_web/Areas/Admin/Models/CompanyViewModel.cs new file mode 100644 index 00000000..e193575e --- /dev/null +++ b/cicm_web/Areas/Admin/Models/CompanyViewModel.cs @@ -0,0 +1,37 @@ +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Cicm.Database; + +namespace cicm_web.Areas.Admin.Models +{ + public class CompanyViewModel + { + public int Id { get; set; } + public string Name { get; set; } + [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] + [DataType(DataType.Date)] + public DateTime? Founded { get; set; } + [DisplayFormat(DataFormatString = "{0:d}")] + [DataType(DataType.Date)] + public DateTime? Sold { get; set; } + public string SoldTo { get; set; } + public string Country { get; set; } + [Required] + public CompanyStatus Status { get; set; } + + [DisplayName("Sold")] + [NotMapped] + public string SoldView => + Status != CompanyStatus.Active && Status != CompanyStatus.Unknown + ? Sold is null + ? "Unknown" + : Sold.Value.ToShortDateString() + : Sold is null + ? SoldTo is null + ? "" + : "Unknown" + : Sold.Value.ToShortDateString(); + } +} \ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/Companies/Index.cshtml b/cicm_web/Areas/Admin/Views/Companies/Index.cshtml index 4a406e71..c196be6d 100644 --- a/cicm_web/Areas/Admin/Views/Companies/Index.cshtml +++ b/cicm_web/Areas/Admin/Views/Companies/Index.cshtml @@ -29,8 +29,8 @@ // Copyright © 2003-2018 Natalia Portillo *******************************************************************************/ } -@using Cicm.Database.Models -@model IEnumerable +@using cicm_web.Areas.Admin.Models +@model IEnumerable @{ ViewData["Title"] = "Companies (Admin)"; @@ -69,7 +69,7 @@ - @foreach(Company item in Model) + @foreach(CompanyViewModel item in Model) { @@ -82,13 +82,13 @@ @Html.DisplayFor(modelItem => item.Status) - @Html.DisplayFor(modelItem => item.Country.Name) + @Html.DisplayFor(modelItem => item.Country) @Html.DisplayFor(modelItem => item.SoldView) - @Html.DisplayFor(modelItem => item.SoldTo.Name) + @Html.DisplayFor(modelItem => item.SoldTo) netcoreapp2.2 - 3.0.99.533 + 3.0.99.535 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website