Link 在部分 Razor 页面中触发了错误的 OnGet
Link in partial Razor Page fires wrong OnGet
我正在使用 razor 页面来显示 ItemT 模型,并在底部插入了一个部分视图以显示所有属性(未在 n:m 关系中分配给该模型)。
PageModel 'FreePropertiesToItemT' 有两个 public 属性:
public ItemT ItemT { get; set; } // the one Item to show
public IList<PropertyIndexViewModel> FreeProperties { get; set; } // all free properties
调用了一个 OnGetAsync(int? id)
方法,效果很好。该页面正确显示所有数据。
视图为每个 属性:
显示一个 link
<a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>
这将创建 link:
<a href="/Inventory/ItemTs/FreePropertiesToItemT?id=1&handler=addProperty">add</a>
这是正确的link(我认为)。路由、id 值和处理程序是正确的,因为 PageModel 中有第二个 OnGet 方法:
public async Task<IActionResult> OnGetaddPropertyAsync(int? id)
但是,link 每次只调用 OnGetAsync(而不是 OnGetaddProppertyAsync),当然,每次 属性!
我错过了什么?
ItemT 型号:
public class ItemT
{
[Key]
public int ItemTID { get; set; }
[Required]
[StringLength(100, MinimumLength = 1)]
[Display(Name = "ItemT")]
public string Title { get; set; }
public bool isActive { get; set; } = true;
public virtual ICollection<ItemTProperty> ItemTProperties { get; set; }
}
自由属性的ViewModel:
public class PropertyIndexViewModel
{
[Key]
public int PropertyID { get; set; }
[Required]
[StringLength(100, MinimumLength = 1)]
public string Title { get; set; }
public bool DefaultsOnly { get; set; }
[Display(Name = "Unit")]
public string Unit { get; set; }
[Display(Name = "Valuetype")]
public string Valuetype { get; set; }
}
要列出一项的页面 T:
@page
@model Inventory.Areas.Inventory.Pages.ItemTs.FreePropertiesToItemTModel
@{
ViewData["Title"] = "FreePropertiesToItemT";
}
<h1>Free Properties</h1>
<div>
<h4>ItemT</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.ItemT.Title)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.ItemT.Title)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.ItemT.isActive)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.ItemT.isActive)
</dd>
</dl>
</div>
<div>
<a asp-page="./Edit" asp-route-id="@Model.ItemT.ItemTID">Edit</a> |
<a asp-page="./Index">Back to List</a>
</div>
<p></p>
<div>
@{
ViewData["FreeProperties"] = true;
}
<partial name="../Properties/_Properties.cshtml" model="Model.FreeProperties" />
</div>
加载的部分:
@using Inventory.DAL.ViewModels
@model IList<PropertyIndexViewModel>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model[0].DefaultsOnly)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Unit)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Valuetype)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DefaultsOnly)
</td>
<td>
@Html.DisplayFor(modelItem => item.Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Valuetype)
</td>
<td>
@if (ViewBag.FreeProperties != null)
{
<a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>
}
</td>
</tr>
}
</tbody>
</table>
页面后面的c#代码:
namespace Inventory.Areas.Inventory.Pages.ItemTs
{
public class FreePropertiesToItemTModel : PageModel
{
private readonly IUnitOfWork _uow;
public FreePropertiesToItemTModel(IUnitOfWork uow)
{
_uow = uow;
}
public ItemT ItemT { get; set; }
public IList<PropertyIndexViewModel> FreeProperties { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
ItemT = await _uow.ItemTRepo.getById((int)id);
if (ItemT == null)
{
return NotFound();
}
FreeProperties = await _uow.PropertyRepo.getFreePropertiesForItemT((int)id);
return Page();
}
public async Task<IActionResult> OnGetaddPropertyAsync(int? id)
{
if( id == null)
{
return NotFound();
}
if(ItemT == null) { return NotFound(); }
await _uow.ItemTRepo.addProperty(ItemT.ItemTID, (int)id);
await _uow.Commit();
return Page();
}
}
}
问题是你的handler名称错误,修改如下:
public async Task<IActionResult> OnGetAddPropertyAsync(int? id)
处理程序名称的第一个字母必须大写,否则 url 中的 handler=addProperty
将被视为查询字符串参数而不是处理程序名称。
我正在使用 razor 页面来显示 ItemT 模型,并在底部插入了一个部分视图以显示所有属性(未在 n:m 关系中分配给该模型)。
PageModel 'FreePropertiesToItemT' 有两个 public 属性:
public ItemT ItemT { get; set; } // the one Item to show
public IList<PropertyIndexViewModel> FreeProperties { get; set; } // all free properties
调用了一个 OnGetAsync(int? id)
方法,效果很好。该页面正确显示所有数据。
视图为每个 属性:
显示一个 link<a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>
这将创建 link:
<a href="/Inventory/ItemTs/FreePropertiesToItemT?id=1&handler=addProperty">add</a>
这是正确的link(我认为)。路由、id 值和处理程序是正确的,因为 PageModel 中有第二个 OnGet 方法:
public async Task<IActionResult> OnGetaddPropertyAsync(int? id)
但是,link 每次只调用 OnGetAsync(而不是 OnGetaddProppertyAsync),当然,每次 属性! 我错过了什么?
ItemT 型号:
public class ItemT
{
[Key]
public int ItemTID { get; set; }
[Required]
[StringLength(100, MinimumLength = 1)]
[Display(Name = "ItemT")]
public string Title { get; set; }
public bool isActive { get; set; } = true;
public virtual ICollection<ItemTProperty> ItemTProperties { get; set; }
}
自由属性的ViewModel:
public class PropertyIndexViewModel
{
[Key]
public int PropertyID { get; set; }
[Required]
[StringLength(100, MinimumLength = 1)]
public string Title { get; set; }
public bool DefaultsOnly { get; set; }
[Display(Name = "Unit")]
public string Unit { get; set; }
[Display(Name = "Valuetype")]
public string Valuetype { get; set; }
}
要列出一项的页面 T:
@page
@model Inventory.Areas.Inventory.Pages.ItemTs.FreePropertiesToItemTModel
@{
ViewData["Title"] = "FreePropertiesToItemT";
}
<h1>Free Properties</h1>
<div>
<h4>ItemT</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.ItemT.Title)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.ItemT.Title)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.ItemT.isActive)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.ItemT.isActive)
</dd>
</dl>
</div>
<div>
<a asp-page="./Edit" asp-route-id="@Model.ItemT.ItemTID">Edit</a> |
<a asp-page="./Index">Back to List</a>
</div>
<p></p>
<div>
@{
ViewData["FreeProperties"] = true;
}
<partial name="../Properties/_Properties.cshtml" model="Model.FreeProperties" />
</div>
加载的部分:
@using Inventory.DAL.ViewModels
@model IList<PropertyIndexViewModel>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model[0].DefaultsOnly)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Unit)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Valuetype)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DefaultsOnly)
</td>
<td>
@Html.DisplayFor(modelItem => item.Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Valuetype)
</td>
<td>
@if (ViewBag.FreeProperties != null)
{
<a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>
}
</td>
</tr>
}
</tbody>
</table>
页面后面的c#代码:
namespace Inventory.Areas.Inventory.Pages.ItemTs
{
public class FreePropertiesToItemTModel : PageModel
{
private readonly IUnitOfWork _uow;
public FreePropertiesToItemTModel(IUnitOfWork uow)
{
_uow = uow;
}
public ItemT ItemT { get; set; }
public IList<PropertyIndexViewModel> FreeProperties { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
ItemT = await _uow.ItemTRepo.getById((int)id);
if (ItemT == null)
{
return NotFound();
}
FreeProperties = await _uow.PropertyRepo.getFreePropertiesForItemT((int)id);
return Page();
}
public async Task<IActionResult> OnGetaddPropertyAsync(int? id)
{
if( id == null)
{
return NotFound();
}
if(ItemT == null) { return NotFound(); }
await _uow.ItemTRepo.addProperty(ItemT.ItemTID, (int)id);
await _uow.Commit();
return Page();
}
}
}
问题是你的handler名称错误,修改如下:
public async Task<IActionResult> OnGetAddPropertyAsync(int? id)
处理程序名称的第一个字母必须大写,否则 url 中的 handler=addProperty
将被视为查询字符串参数而不是处理程序名称。