2

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!

3
  • I was following the same tutorial. I tried an option that was given by a user and the pop-up works but it only shows the same image always. Anyone? Commented Feb 4, 2020 at 9:27
  • 2
    Please go through this section. I hope that this will help you. learn.microsoft.com/en-in/aspnet/core/tutorials/… Commented Feb 4, 2020 at 10:35
  • 1
    Hi @KesavanSubramaniam thank you very much for your comment. However, for some reason, @onclick does not work for me. I have installed the dotnet version 3.1.101 Commented Feb 4, 2020 at 12:11

1 Answer 1

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me and should be the selected answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.