Use view model for owned machine index.

This commit is contained in:
2019-05-29 23:37:51 +01:00
parent b9a8c7e832
commit e48817a5f1
5 changed files with 62 additions and 66 deletions

View File

@@ -29,6 +29,8 @@
*******************************************************************************/ *******************************************************************************/
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
// ReSharper disable UnusedMember.Global
// ReSharper disable InconsistentNaming
namespace Cicm.Database namespace Cicm.Database
{ {
@@ -47,9 +49,13 @@ namespace Cicm.Database
public enum StatusType public enum StatusType
{ {
[Display(Name = "Unknown")]
Unknown = 0, Unknown = 0,
[Display(Name = "Tested good")]
TestedGood = 1, TestedGood = 1,
[Display(Name = "Not tested")]
NotTested = 2, NotTested = 2,
[Display(Name = "Tested bad")]
TestedBad = 3 TestedBad = 3
} }

View File

@@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cicm.Database.Models; using Cicm.Database.Models;
using cicm_web.Areas.Admin.Models;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
@@ -22,7 +23,15 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine // GET: OwnedMachine
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var cicmContext = _context.OwnedMachines.Include(o => o.Machine); var cicmContext = _context.OwnedMachines.Include(o => o.Machine).OrderBy(o => o.Machine.Company.Name).ThenBy(o => o.Machine.Name).ThenBy(o => o.User.UserName).ThenBy(o => o.AcquisitionDate).Select(o => new OwnedMachineViewModel
{
AcquisitionDate = o.AcquisitionDate,
Id = o.Id,
Machine = $"{o.Machine.Company.Name} {o.Machine.Name}",
Status = o.Status,
User = o.User.UserName
});
return View(await cicmContext.ToListAsync()); return View(await cicmContext.ToListAsync());
} }

View File

@@ -0,0 +1,17 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Cicm.Database;
namespace cicm_web.Areas.Admin.Models
{
public class OwnedMachineViewModel : BaseViewModel<long>
{
[DataType(DataType.Date)]
[DisplayName("Acquired")]
public DateTime AcquisitionDate { get; set; }
public StatusType Status { get; set; }
public string Machine { get; set; }
public string User { get; set; }
}
}

View File

@@ -1,89 +1,53 @@
@model IEnumerable<Cicm.Database.Models.OwnedMachine> @model IEnumerable<cicm_web.Areas.Admin.Models.OwnedMachineViewModel>
@{ @{
ViewData["Title"] = "Index"; ViewData["Title"] = "Index";
} }
<h1>Index</h1> <h1>Owned machines</h1>
<p> <p>
<a asp-action="Create">Create New</a> <a asp-action="Create" class="btn btn-primary">Create New</a>
</p> </p>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th> <th>
@Html.DisplayNameFor(model => model.AcquisitionDate) @Html.DisplayNameFor(model => model.Machine)
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.LostDate) @Html.DisplayNameFor(model => model.User)
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.Status) @Html.DisplayNameFor(model => model.AcquisitionDate)
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.LastStatusDate) @Html.DisplayNameFor(model => model.Status)
</th> </th>
<th> <th></th>
@Html.DisplayNameFor(model => model.Trade) </tr>
</th>
<th>
@Html.DisplayNameFor(model => model.Boxed)
</th>
<th>
@Html.DisplayNameFor(model => model.Manuals)
</th>
<th>
@Html.DisplayNameFor(model => model.SerialNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.SerialNumberVisible)
</th>
<th>
@Html.DisplayNameFor(model => model.Machine)
</th>
<th></th>
</tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var item in Model) { @foreach (var item in Model) {
<tr> <tr>
<td> <td>
@Html.DisplayFor(modelItem => item.AcquisitionDate) @Html.DisplayFor(modelItem => item.Machine)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.LostDate) @Html.DisplayFor(modelItem => item.User)
</td>
<td>
@Html.DisplayFor(modelItem => item.AcquisitionDate)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.Status) @Html.DisplayFor(modelItem => item.Status)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.LastStatusDate) <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">Details</a>
</td> <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">Edit</a>
<td> <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">Delete</a>
@Html.DisplayFor(modelItem => item.Trade)
</td>
<td>
@Html.DisplayFor(modelItem => item.Boxed)
</td>
<td>
@Html.DisplayFor(modelItem => item.Manuals)
</td>
<td>
@Html.DisplayFor(modelItem => item.SerialNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.SerialNumberVisible)
</td>
<td>
@Html.DisplayFor(modelItem => item.Machine.Name)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td> </td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.676</Version> <Version>3.0.99.679</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>