我有一个结构如下的应用程序: 表 1 = 客户端 表 2 = 面板
每个客户端可以有多个面板。当我提交“创建客户端”页面时,我的应用程序将导航到并打开“创建面板”页面(将新的 Client.Id 传递到“创建面板”页面)。当我提交“创建面板”页面时,它会返回到“编辑客户端”页面,您可以在其中查看“编辑客户端”页面内的面板。从那里,您可以更新“编辑客户端”页面并返回到“客户端索引”,或者如果您没有更改“编辑客户端”屏幕的客户端部分,则仅返回到“客户端索引”。这一切都有效。
我的问题是,在“编辑客户端”屏幕的每个面板上,都有“编辑”和“删除面板”按钮。我使用它们导航到该特定客户端的特定面板并进行更改或删除该面板。我遇到的问题是,我无法将 ClientId 从“编辑面板”页面传回“编辑客户端”页面。我收到 NULL 异常。
我知道我错过了一些东西,但是当我尝试将“clientid”添加到“编辑客户端 OnGet”时,它永远不会到达代码的该部分,因为它读取 .GetAttendees 部分,我认为,并且永远不会到达“clientid” 。我已经设置了断点,可以看到问题是 Client.Id 本身没有被传递,只是不知道执行此操作的正确语法。
以下是我的代码摘录:
客户端.Edit.cshtml
@page
@model SampleApp.Pages.Clients.EditModel
<form method="post">
<input hidden asp-for="Client.Id" />
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary pl-3">Edit Client</h2>
<hr />
</div>
<div asp-validation-summary="All"></div>
<h4>Demographics</h4>
<br />
<table class="table table-borderless" style="width:100%">
<tr>
<td style="width: 34%">
<div class="mb-3">
<label asp-for="Client.ClientFirstName"></label>
<input asp-for="Client.ClientFirstName" class="form-control" />
<span asp-validation-for="Client.ClientFirstName" class="text-danger"></span>
</div>
</td>
<td style="width: 34%">
<div class="mb-3">
<label asp-for="Client.ClientLastName"></label>
<input asp-for="Client.ClientLastName" class="form-control" />
<span asp-validation-for="Client.ClientLastName" class="text-danger"></span>
</div>
</td>
</tr>
</table>
<hr />
<h4>Assigned Panels</h4>
<table>
<tr>
<td>
<div class="w-75 btn-group" role="group">
<a asp-page="/Attendances/Create" asp-route-clientid="@Model.Client.Id" class="btn btn-primary mx-2" style="width:300px;">
Add Panel Attendance
</a>
</div>
</td>
</tr>
</table>
<br />
<table class="table table-bordered table-striped width:100%">
<thead>
<tr>
<th>
Panel Id
</th>
<th>
Panel Assigned
</th>
<th>
Update Panel
</th>
<th>
Delete Panel
</th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.Client.GetAttendees.Count; i++)
{
<tr>
<td style="width: 10%">
<div class="mb-3">
<input asp-for="Client.GetAttendees[i].ClientId" type="hidden" />
<input asp-for="Client.GetAttendees[i].Id" type="text" readonly class="form-control-plaintext" />
</div>
</td>
<td style="width: 30%">
<div class="mb-3">
<input asp-for="Client.GetAttendees[i].PanelAssigned" type="text" readonly class="form-control-plaintext" />
</div>
</td>
<td width="10%">
<div class="w-75 btn-group" role="group">
<a asp-page="/Attendances/Edit" class="btn btn-primary mx-2" asp-route-id="@Model.Client.GetAttendees[i].Id" asp-route-id="@Model.Client.GetAttendees[i].ClientId">
<i class="bi-clipboard2-pulse"></i>
</a>
</div>
</td>
<td width="10%">
<div class="w-75 btn-group" role="group">
<a asp-page="/Attendances/Delete" class="btn btn-danger mx-2" asp-route-id="@Model.Client.GetAttendees[i].Id" asp-route-id="@Model.Client.GetAttendees[i].ClientId">
<i class="bi bi-trash"></i>
</a>
</div>
</td>
</tr>
}
</table>
<div>
<button type="submit" class="btn btn-primary" style="width:300px;">Update -> Return to Client List</button>
<a asp-page="Index" class="btn btn-secondary" style="width: 300px;">Return to Client List</a>
</div>
</div>
</form>
@section Scripts
{
<partial name="_ValidationScriptsPartial"/>
}
客户端.Edit.cs
namespace SampleApp.Pages.Clients;
[BindProperties]
public class EditModel : PageModel
{
private readonly ApplicationDbContext _db;
public Client Client { get; set; }
public Attendance Attendance { get; set; }
public EditModel(ApplicationDbContext db)
{
_db = db;
}
public void OnGet(int id)
{
Client = _db.Client
.Include(client => client.GetAttendees)
.FirstOrDefault(client => client.Id == id);
}
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
_db.Update(Client);
await _db.SaveChangesAsync();
TempData["success"] = "Client updated successfully.";
return RedirectToPage("/Clients/Index");
}
return Page();
}
}
出勤.Edit.cshtml
@page
@model DWIVIP.Pages.Attendances.EditModel
<form method="post">
<input hidden asp-for="Attendance.Id" />
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary pl-3">Edit Client Attendance</h2>
<hr />
</div>
<div asp-validation-summary="All"></div>
<table class="table table-borderless" style="width:100%">
<tr>
<td style="width: 20%">
<div class="mb-3">
<label asp-for="Attendance.ClientId"></label>
<input asp-for="Attendance.ClientId" name="clientid" class="form-control"/>
<span asp-validation-for="Attendance.ClientId" class="text-danger"></span>
</div>
</td>
<td style="width: 80%">
<div class="mb-3">
<label asp-for="Attendance.PanelAssigned"></label>
<select asp-for="Attendance.PanelAssigned" id="Select1" class="form-select">
<option value="">---Select Panel---</option>
@if (Model.DisplayPanelData != null)
{
@foreach (var item1 in Model.DisplayPanelData.OrderBy(x => x.PanelName))
{
<option value="@item1.PanelName" selected="@(item1.PanelName.ToString()==Model.Attendance.PanelAssigned?true:false)">@item1.PanelName</option>
}
}
</select>
</div>
</td>
</tr>
</table>
<div>
<button type="submit" class="btn btn-primary" style="width:300px;">Update -> Return to Client</button>
</div>
</div>
</form>
@section Scripts
{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
出勤.编辑.cs
namespace SampleApp.Pages.Attendances;
[BindProperties]
public class EditModel : PageModel
{
private readonly ApplicationDbContext _db;
public Attendance Attendance { get; set; } = new Attendance();
public Client Client { get; set; } = new Client();
public EditModel(ApplicationDbContext db)
{
_db = db;
}
public IEnumerable<Panel> DisplayPanelData { get; set; }
public void OnGet(int id)
{
Attendance = _db.Attendance.Find(id);
DisplayPanelData = _db.Panel.ToList();
}
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
_db.Update(Attendance);
await _db.SaveChangesAsync();
TempData["success"] = "Client Attendance updated successfully.";
ViewData["clientid3"] = Attendance.ClientId;
return RedirectToPage("/Clients/Edit", new { id = ViewData["clientid3"] });
}
return Page();
}
}
客户型号:
namespace SampleApp.Model
{
public class Client
{
[Key]
[Required]
public int Id { get; set; }
[ForeignKey("ClientId")]
public List<Attendance>? GetAttendees { get; set; }
[Display(Name = "Client First Name")]
public string? ClientFirstName { get; set; }
[Display(Name = "Client Middle Initial")]
public string? ClientMI { get; set; }
[Display(Name = "Client Last Name")]
public string? ClientLastName { get; set; }
}
}
考勤模式:
namespace DWIVIP.Model
{
public class Attendance
{
[Key]
[Required]
public int Id { get; set; }
[Display(Name ="Client Id")]
public int? ClientId { get; set; }
public Client? Client { get; set; }
[Display(Name = "Panel Assigned")]
public string? PanelAssigned { get; set; }
}
}
感谢您提供的所有帮助。我的删除面板页面似乎也有同样的问题。从我认为错误的角度来看,我根本不明白如何为值的多个实例编写 OnGet?
想通了。正如我经常发现的那样,这是最微小的事情。只需要从中删除 name="clientid" 即可
在Attendances.Edit.cshtml页面上。我一定是在一次尝试解决问题的迭代中把它放在那里,然后忘记了它在那里,直到我开始逐行梳理。