79426079

Date: 2025-02-10 04:37:28
Score: 0.5
Natty:
Report link

Super simple and reusable when you use the JavaScript [JSImport]/[JSExport] interop with ASP.NET Core Blazor (https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/import-export-interop). This is a really good way to create reusable code that interacts with JavaScript libraries.

[SupportedOSPlatform("browser")]
public static partial class AlertInterop
{
    [JSImport("globalThis.alert")]
    public static partial void Alert(string message);
}
 @page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>
<button class="btn btn-primary btn-danger" onclick="if (confirm('Are you sure to Decrement')) { @DecrementCount() }">Decrement</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

    private void DecrementCount()
    {
        currentCount--;
        AlertInterop.Alert('Operation Successfully executed')
    }
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: thecoolestname36