在Blazor/Razor中单击按钮时调用函数-ASP.NET Core 3 [英] Call a function on button click in Blazor/Razor - ASP.NET Core 3

查看:78
本文介绍了在Blazor/Razor中单击按钮时调用函数-ASP.NET Core 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习ASP.NET Core,并且正在关注Microsoft的YouTube教程(

I just started learning ASP.NET Core and I was following the YouTube tutorial from Microsoft (https://www.youtube.com/watch?v=wA-3FA2kbpA&list=PLdo4fOcmZ0oW8nviYduHq7bmKode-p8Wy&index=11) and when I tried to call the function on button click, it does not work.

The code is as follows:

Button in HTML

<button @onclick="(e => SelectProduct(product.Id))" data-toggle="modal" data-target="#productModal" class="btn btn-primary">More Info</button>

@Code Element - Function

void SelectProduct(string productId)
{
    selectedProductId = productId;
    selectedProduct = ProductService.GetProducts().First(x => x.Id == productId);
}

Kindly help me debug this. The function does not get called when the button is clicked.
Thanks in advance!

解决方案

However, for some reason, @onclick does not work for me.

It seems that you are integrating blazor into MVC / Razor Pages project.Based on this thread I have answered before, you need to add a new _Imports.razor file in your Components folder if you put your razor components there.

_Imports.razor:

@using Microsoft.AspNetCore.Components.Web

这篇关于在Blazor/Razor中单击按钮时调用函数-ASP.NET Core 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆