Implemented /Home/Location table and delete action

This commit is contained in:
2025-10-10 09:35:53 +02:00
parent 432a28271b
commit 1e13ea11d7
6 changed files with 67 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
@using Microsoft.AspNetCore.Mvc.Localization
@using Berufsschule_HAM.Models
@model HomeIndexViewModel
@model LocationsIndexViewModel
@inject IViewLocalizer T
@{
ViewData["Title"] = T["Locations"];
@@ -28,19 +28,19 @@
</tr>
</thead>
<tbody>
@* @{
foreach (AssetsTableViewModel assetsTableViewModel in Model.AssetsTableViewModels)
@{
foreach (LocationTableViewModel locationTableViewModel in Model.LocationTableViewModels)
{
<tr>
<td>@assetsTableViewModel.UserUID</td>
<td>@assetsTableViewModel.AssetCn</td>
<td>@assetsTableViewModel.AssetName</td>
<td>@assetsTableViewModel.LocationName</td>
<td>@locationTableViewModel.LocationID</td>
<td>@locationTableViewModel.LocationName</td>
<td>@locationTableViewModel.RoomNumber</td>
<td>@locationTableViewModel.Seat</td>
<td>
<div class="d-flex gap-2">
<button class="btn btn-sm btn-primary">Update</button>
<button class="btn btn-sm btn-danger btn-delete"
data-asset-id="@assetsTableViewModel.AssetCn"
data-location-id="@locationTableViewModel.LocationID"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
🗑️ Delete
@@ -49,13 +49,13 @@
</td>
</tr>
}
} *@
}
</tbody>
</table>
</div>
</div>
<!-- Asset Delete Confirmation Modal -->
<!-- Location Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
@@ -64,7 +64,7 @@
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete the asset <strong id="assetName"></strong> (ID: <span id="assetId"></span>)?</p>
<p>Are you sure you want to delete the location <strong id="locationName"></strong> (ID: <span id="locationId"></span>)?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
@@ -77,21 +77,21 @@
</div>
@* <script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const deleteModal = document.getElementById('deleteModal');
let currentButton = null; // The delete button that opened the modal
deleteModal.addEventListener('show.bs.modal', event => {
currentButton = event.relatedTarget; // Button that triggered the modal
const assetId = currentButton.getAttribute('data-asset-id');
const assetName = currentButton.getAttribute('data-asset-name');
const locationId = currentButton.getAttribute('data-location-id');
const locationName = currentButton.getAttribute('data-location-name');
deleteModal.querySelector('#assetId').textContent = assetId;
deleteModal.querySelector('#assetName').textContent = assetName;
deleteModal.querySelector('#locationId').textContent = locationId;
deleteModal.querySelector('#locationName').textContent = locationName;
// Store the delete URL for later use
deleteModal.querySelector('#deleteForm').dataset.url = `/Assets/Delete?cn=${assetId}`;
deleteModal.querySelector('#deleteForm').dataset.url = `/Locations/Delete?cn=${locationId}`;
});
// Handle submit of deleteForm via fetch()
@@ -100,7 +100,7 @@
e.preventDefault();
const url = deleteForm.dataset.url;
const assetId = deleteModal.querySelector('#assetId').textContent;
const locationId = deleteModal.querySelector('#locationId').textContent;
try {
const response = await fetch(url, {
@@ -109,7 +109,7 @@
'Content-Type': 'application/json',
'Accept': 'application/json'
}//,
//body: JSON.stringify({ id: assetId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
//body: JSON.stringify({ id: locationId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
});
const result = await response.json();
@@ -124,7 +124,7 @@
row.classList.add('table-danger');
setTimeout(() => row.remove(), 300);
showToast('Asset deleted successfully', 'success');
showToast('Location deleted successfully', 'success');
} else {
showToast(`❌ ${result.reason}: ${result.exception || 'Unknown error'}`, 'danger');
}
@@ -160,4 +160,4 @@
return container;
}
});
</script> *@
</script>