asp.net 核心组件中的@OnClick 未触发目标方法
in asp.net core Component @OnClick not trigger target method
我尝试使用我的存储库数据在卡片视图中显示,然后按按钮查看有关该项目的更多信息,但它不起作用。 @OnClick 仅适用于 JSON 数据
@using Microsoft.AspNetCore.Components.Web
- 访问 @onclick
和更多选项
repoItem
- 我的 ItemRepository 用于从数据库获取数据
@OnClick="(e => SelectProduct(item.Id))"
- 当我点击物品卡片时,它应该将物品 ID 发送到 SelectProduct(item.Id)
方法。
但它适用于关注 link。他使用 JSON 数据,但我需要使用模型数据。
https://github.com/dotnet-presentations/ContosoCrafts/blob/master/src/Components/ProductList.razor
<div class="card-columns">
@foreach (var item in repoItem.GetAll())
{
<div class="card">
<div class="card-header">
<h5 class="card-title">@item.Name</h5>
</div>
<div class="card-body">
<h5 class="card-title"> Total available items : @item.Quantity</h5>
<h5 class="card-title">Price : Rs. @item.Price.00</h5>
</div>
<div class="card-footer">
<small class="text-muted">
<button @onclick="(e => SelectProduct(item.Id))"
data-toggle="modal" data-target="#productModal" class="btn btn-primary">
More Info
</button>
</small>
</div>
</div>
}
</div>
@code {
Item selectedItem;
int selectedItemId;
void SelectProduct(int productId)
{
selectedItemId = productId;
selectedItem = _context.Items.Where(x => x.Id == selectedItemId).FirstOrDefault();
ContItem();
}
int itemcnt = 0;
string cuntLable;
void ContItem()
{
if (selectedItem.Quantity != null || selectedItem.Quantity != 0)
{
itemcnt = selectedItem.Quantity;
cuntLable = itemcnt.ToString();
}
else cuntLable = "Not available ..!";
}
}
问题:@onclick
=".." 在单击按钮时未命中我的 selectprodect
方法断点。
解决方法:错误是Statup.cs
需要在ConfigureServices
中添加services.AddServerSideBlazor()
然后在Configure
部分添加endpoints.MapBlazorHub()
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
经过 6 个小时的努力。 @onclick 运行顺利
我尝试使用我的存储库数据在卡片视图中显示,然后按按钮查看有关该项目的更多信息,但它不起作用。 @OnClick 仅适用于 JSON 数据
@using Microsoft.AspNetCore.Components.Web
- 访问 @onclick
和更多选项
repoItem
- 我的 ItemRepository 用于从数据库获取数据
@OnClick="(e => SelectProduct(item.Id))"
- 当我点击物品卡片时,它应该将物品 ID 发送到 SelectProduct(item.Id)
方法。
但它适用于关注 link。他使用 JSON 数据,但我需要使用模型数据。
https://github.com/dotnet-presentations/ContosoCrafts/blob/master/src/Components/ProductList.razor
<div class="card-columns">
@foreach (var item in repoItem.GetAll())
{
<div class="card">
<div class="card-header">
<h5 class="card-title">@item.Name</h5>
</div>
<div class="card-body">
<h5 class="card-title"> Total available items : @item.Quantity</h5>
<h5 class="card-title">Price : Rs. @item.Price.00</h5>
</div>
<div class="card-footer">
<small class="text-muted">
<button @onclick="(e => SelectProduct(item.Id))"
data-toggle="modal" data-target="#productModal" class="btn btn-primary">
More Info
</button>
</small>
</div>
</div>
}
</div>
@code {
Item selectedItem;
int selectedItemId;
void SelectProduct(int productId)
{
selectedItemId = productId;
selectedItem = _context.Items.Where(x => x.Id == selectedItemId).FirstOrDefault();
ContItem();
}
int itemcnt = 0;
string cuntLable;
void ContItem()
{
if (selectedItem.Quantity != null || selectedItem.Quantity != 0)
{
itemcnt = selectedItem.Quantity;
cuntLable = itemcnt.ToString();
}
else cuntLable = "Not available ..!";
}
}
问题:@onclick
=".." 在单击按钮时未命中我的 selectprodect
方法断点。
解决方法:错误是Statup.cs
需要在ConfigureServices
中添加services.AddServerSideBlazor()
然后在Configure
部分添加endpoints.MapBlazorHub()
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
经过 6 个小时的努力。 @onclick 运行顺利