I try to use my repository data to display in card view, then press button to see more information about the item, it does not work. @OnClick is only working for JSON data
@using Microsoft.AspNetCore.Components.Web
- to access @onclick
and more option
repoItem
- my ItemRepository for get data from database
@OnClick="(e => SelectProduct(item.Id))"
- when i click item card, its shoud get item id send to SelectProduct(item.Id)
method.
but it work for following link. he works with JSON data but I need to work for model data.
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 ..!";
}
}
problem: @onclick
=".." is not hit my selectprodect
method breakpoint when clicking the button.
Solution: the mistake is Statup.cs
need to add services.AddServerSideBlazor()
in ConfigureServices
and then add in Configure
part endpoints.MapBlazorHub()
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
after 6 hours of hard work. @onclick is worked smoothly