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;
// ReSharper disable UnusedMember.Global
// ReSharper disable InconsistentNaming
namespace Cicm.Database
{
@@ -47,9 +49,13 @@ namespace Cicm.Database
public enum StatusType
{
[Display(Name = "Unknown")]
Unknown = 0,
[Display(Name = "Tested good")]
TestedGood = 1,
[Display(Name = "Not tested")]
NotTested = 2,
[Display(Name = "Tested bad")]
TestedBad = 3
}

View File

@@ -1,6 +1,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;
@@ -22,7 +23,15 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine
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());
}

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";
}
<h1>Index</h1>
<h1>Owned machines</h1>
<p>
<a asp-action="Create">Create New</a>
<a asp-action="Create" class="btn btn-primary">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.AcquisitionDate)
</th>
<th>
@Html.DisplayNameFor(model => model.LostDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th>
@Html.DisplayNameFor(model => model.LastStatusDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Trade)
</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>
<tr>
<th>
@Html.DisplayNameFor(model => model.Machine)
</th>
<th>
@Html.DisplayNameFor(model => model.User)
</th>
<th>
@Html.DisplayNameFor(model => model.AcquisitionDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.AcquisitionDate)
@Html.DisplayFor(modelItem => item.Machine)
</td>
<td>
@Html.DisplayFor(modelItem => item.LostDate)
@Html.DisplayFor(modelItem => item.User)
</td>
<td>
@Html.DisplayFor(modelItem => item.AcquisitionDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastStatusDate)
</td>
<td>
@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>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">Details</a>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">Edit</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">Delete</a>
</td>
</tr>
}
}
</tbody>
</table>

View File

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