我需要别人帮我修改作业。
我正在使用Blazor Web Assembly Standalone来构建这个名为CarRentalManagement
的应用程序。
我已经为车辆页面创建了数据结构。但我无法访问该页面。
enter image description here
页面应该是可访问的,看起来像这样:
enter image description here的
我怀疑这可能是我的.NET错误。你们有什么想法吗?
谢谢您的支持:)Vehicle.cs
(在CarRentalManagement.Shared
的Domains
文件夹中):
namespace CarRentalManagement.Shared.Domain
{
public class Vehicle : BaseDomainModel
{
public int Year { get; set; }
public string? LicensePlateNumber { get; set; }
public int MakeId { get; set; }
public virtual Make? Make { get; set; }
public int ModelId { get; set; }
public virtual Model? Model { get; set; }
public int ColourId { get; set; }
public virtual Colour? Colour { get; set; }
public virtual List<Booking>? Bookings { get; set; }
}
}
字符串Index.razor
(在CarRentalManagement.Client
的Vehicles
文件夹中):
@page "/vehicles/"
@inject HttpClient _client
@inject IJSRuntime js
@attribute [Authorize]
<h3 class="card-title">Car Vehicles</h3>
<br />
@if (Vehicles == null)
{
<div class="alert alert-info">Loading Vehicles...</div>
}
else
{
<table class="table table-responsive">
<thead>
<tr>
<th>Make</th>
<th>Model</th>
<th>Year</th>
<th>License Plate Number</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var vehicle in Vehicles)
{
<tr>
<td>@vehicle.Make.Name</td>
<td>@vehicle.Model.Name</td>
<td>@vehicle.Year</td>
<td>@vehicle.LicensePlateNumber </td>
<td>
<a href="/vehicles/view/@vehicle.Id" class="btn btn-primary">
<span class="oi oi-book"></span>
</a>
<a href="/vehicles/edit/@vehicle.Id" class="btn btn-warning">
<span class="oi oi-pencil"></span>
</a>
<button class="btn btn-danger" @onclick="@(()=>Delete(vehicle.Id))">
<span class="oi oi-delete"></span>
</button>
</td>
</tr>
}
</tbody>
</table>
}
@code {
private List<Vehicle>? Vehicles;
protected async override Task OnInitializedAsync()
{
Vehicles = await _client.GetFromJsonAsync<List<Vehicle>>($"{Endpoints.VehiclesEndpoint}");
}
async Task Delete(int vehicleId)
{
var vehicle = Vehicles.First(q => q.Id == vehicleId);
var confirm = await js.InvokeAsync<bool>("confirm", $"Do you want to delete {vehicle.LicensePlateNumber}?");
if (confirm)
{
await _client.DeleteAsync($"{Endpoints.VehiclesEndpoint}/{vehicleId}");
await OnInitializedAsync();
}
}
}
型VehiclesController.cs
:
namespace CarRentalManagement.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class VehiclesController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
public VehiclesController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
// GET: api/Vehicles
[HttpGet]
public async Task<IActionResult> GetVehicles()
{
if (_unitOfWork.Vehicles == null)
{
return NotFound();
}
var vehicles = await _unitOfWork.Vehicles
.GetAll(includes: q => q.Include(x => x.Make).Include(x => x.Model).Include(x => x.Colour));
return Ok(vehicles);
}
// GET: api/Vehicles/5
[HttpGet("{id}")]
public async Task<IActionResult> GetVehicle(int id)
{
if (_unitOfWork.Vehicles == null)
{
return NotFound();
}
var vehicle = await _unitOfWork.Vehicles.Get(q => q.Id == id);
if (vehicle == null)
{
return NotFound();
}
return Ok(vehicle);
}
// PUT: api/Vehicles/5
[HttpPut("{id}")]
public async Task<IActionResult> PutVehicle(int id, Vehicle vehicle)
{
if (id != vehicle.Id)
{
return BadRequest();
}
_unitOfWork.Vehicles.Update(vehicle);
try
{
await _unitOfWork.Save(HttpContext);
}
catch (DbUpdateConcurrencyException)
{
if (!await VehicleExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Vehicles
[HttpPost]
public async Task<ActionResult<Vehicle>> PostVehicle(Vehicle vehicle)
{
if (_unitOfWork.Vehicles == null)
{
return Problem("Entity set 'ApplicationDbContext.Vehicles' is null.");
}
await _unitOfWork.Vehicles.Insert(vehicle);
await _unitOfWork.Save(HttpContext);
return CreatedAtAction("GetVehicle", new { id = vehicle.Id }, vehicle);
}
// DELETE: api/Vehicles/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteVehicle(int id)
{
if (_unitOfWork.Vehicles == null)
{
return NotFound();
}
var vehicle = await _unitOfWork.Vehicles.Get(q => q.Id == id);
if (vehicle == null)
{
return NotFound();
}
await _unitOfWork.Vehicles.Delete(id);
await _unitOfWork.Save(HttpContext);
return NoContent();
}
private async Task<bool> VehicleExists(int id)
{
var vehicle = await _unitOfWork.Vehicles.Get(q => q.Id == id);
return vehicle != null;
}
}
}
型
1条答案
按热度按时间rjee0c151#
这是合乎逻辑的,如果你得到这样一个页面,那么在
Index.razor
中,你的Vehicles
等于null
。尝试首先检查您是否有
Vehicles
,并在OnInitializedAsync()
方法中接收它们。然后尝试在此方法结束时调用StateHasChanged()
方法重新呈现组件。也许这会有所帮助。P.S.我对Blazor没有太多的经验。