Nei*_*l W 16 razor razor-pages
我有一个显示公司和员工列表的剃刀页面:
@page "/admin/companies/editor/{id}"
@model EditorModel
@{
}
<h1>admin . companies . editor</h1>
<h4>Id = @Model.Id</h4>
<h4>Name = @Model.OrgStructure.Organisation.Name</h4>
<h4>Staff Count = @Model.OrgStructure.Staff.Count</h4>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
@if (Model.OrgStructure.Staff.Any())
{
foreach (Staff s in Model.OrgStructure.Staff)
{
<tr>
<td>@s.Id</td>
<td>@s.Username</td>
</tr>
}
}
else
{
<tr>
<td colspan="2">No Staff</td>
</tr>
}
</table>
<a class="btn btn-primary" asp-page="/admin/companies/staff/create" asp-route-orgId="@Model.OrgStructure.Organisation.Id">Create Staff</a>
@functions {
public class EditorModel : PageModel
{
public string Id { get; set; }
public OrganisationStructure OrgStructure { get; set; }
public void OnGet(string id)
{
Id = id;
using (DirectoryApp app = ServicesFactory.MakeDirectoryService())
{
OrgStructure = app.GetOrganisationStructureAsync(new Guid(id)).Result;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我点击时:
<a class="btn btn-primary" asp-page="/admin/companies/staff/create" asp-route-orgId="@Model.OrgStructure.Organisation.Id">Create Staff</a>
Run Code Online (Sandbox Code Playgroud)
它带我进入我的员工创建页面:
@page "/admin/companies/staff/create/{orgId}"
@model CreateModel
@{
}
<h1>admin . companies . staff . create</h1>
<h3>OrganisationId = @Model.OrganisationId</h3>
<form method="post">
<input name="OrgId" value="@Model.OrganisationId" />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label>User Name</label>
<input name="UserName" class="form-control" value="@Model.UserName" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-secondary" asp-page="/admin/companies/editor" asp-route-id="@Model.OrganisationId">Back</a>
</form>
@functions {
public class CreateModel : PageModel
{
[BindProperty]
[Required]
public string UserName { get; set; }
public Guid OrganisationId { get; set; }
public void OnGet(string orgId)
{
OrganisationId = new Guid(orgId);
}
public async Task<IActionResult> OnPostAsync(string userName, string orgId)
{
if (ModelState.IsValid)
{
using (DirectoryApp app = ServicesFactory.MakeDirectoryService())
{
await app.AddStaffToOrganisationAsync(
new Guid(orgId),
new Staff()
{
Username = userName
});
return RedirectToPage("/admin/companies/editor/" + orgId.ToString());
}
}
return Page();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
如果我单击“提交”并出现模型错误,则会收到错误消息,但使用 @Model.OrganizationId 的字段将被清空。我猜想当在 OnGet() 方法中设置 OrganizationId 时,它会在发布后丢失。这是否意味着我需要在 OnPostAsync() 方法中重新填充我的 CreateModel.OrganizationId ?
如果我成功的话:
返回 RedirectToPage("/admin/companies/editor/" + orgId.ToString());
应该带我回到原来的屏幕,查看新添加的员工的公司。但是,我收到错误:
处理请求时发生未处理的异常。InvalidOperationException:没有名为“/admin/companies/editor/473be1aa-7d62-4964-b744-b34e0489d7ad”的页面与提供的值匹配。
但是,如果我将“/admin/companies/editor/473b...”复制并粘贴到浏览器地址栏中的“localhost:12345”之后,则页面打开没有问题。
我究竟做错了什么?
我需要更换:
return RedirectToPage("/admin/companies/editor/" + orgId.ToString());
Run Code Online (Sandbox Code Playgroud)
和
return RedirectToPage("/admin/companies/editor", new { id = orgId.ToString() });
Run Code Online (Sandbox Code Playgroud)
如果我单击“提交”并出现模型错误,则会收到错误消息,但使用 @Model.OrganizationId 的字段将被清空。我猜想当在 OnGet() 方法中设置 OrganizationId 时,它会在发布后丢失。这是否意味着我需要在 OnPostAsync() 方法中重新填充我的 CreateModel.OrganizationId ?
对此的答案是:
所以:
<input name="OrgId" value="@Model.OrganisationId" />
Run Code Online (Sandbox Code Playgroud)
更改为:
<input name="OrganisationId" value="@Model.OrganisationId" />
Run Code Online (Sandbox Code Playgroud)
和
public async Task<IActionResult> OnPostAsync(string userName, string orgId)
{
if (ModelState.IsValid)
{
using (DirectoryApp app = ServicesFactory.MakeDirectoryService())
{
await app.AddStaffToOrganisationAsync(
new Guid(orgId),
new Staff()
{
Username = userName
});
return RedirectToPage("/admin/companies/editor/" + orgId.ToString());
}
}
return Page();
}
Run Code Online (Sandbox Code Playgroud)
更改为:
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
using (DirectoryApp app = ServicesFactory.MakeDirectoryService())
{
await app.AddStaffToOrganisationAsync(
OrganisationId,
new Staff()
{
Username = this.UserName
});
return RedirectToPage("/admin/companies/editor", new { id = OrganisationId.ToString() });
}
}
return Page();
}
Run Code Online (Sandbox Code Playgroud)
和
[BindProperty]
[Required]
public string UserName { get; set; }
public Guid OrganisationId { get; set; }
Run Code Online (Sandbox Code Playgroud)
更改为
[BindProperty]
[Required]
public string UserName { get; set; }
[BindProperty]
public Guid OrganisationId { get; set; }
Run Code Online (Sandbox Code Playgroud)
使用上面的:
在模型验证失败的帖子上,属性在之后仍保留在 UI 上
返回页面();
现在,我已将参数作为匿名对象移动到第二个参数,而不是连接字符串作为查询参数,因此重定向到页面可以正常工作。
| 归档时间: |
|
| 查看次数: |
13402 次 |
| 最近记录: |