Thursday, May 23, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  89] [ 2]  / answers: 1 / hits: 7963  / 4 Years ago, sat, august 1, 2020, 12:00:00

I am trying create an alert message in within a Blazor component. I have no idea how to do this. I am running ASP.NET Core 3.1 Blazor server-side. Here's what I've tried


Component function:


private async Task ShowAlert()
{
await JSRuntime.InvokeAsync<object>("ShowMsg");
}

Javascript Interop:


function ShowMsg() {
success = "Success!";
return success;
}

File host.cshtml:


 <script src="~/BlazorInterop.js"></script>


More From » c#

 Answers
4
@page "/"

<button @onclick="MessageBox">Show Message</button>

@code
{
[Inject] IJSRuntime JSRuntime { get; set; }
protected async Task MessageBox()
{
await JSRuntime.InvokeVoidAsync("exampleJsFunctions.ShowMsg",
"Hello Blazor");
}
}

Put the following script tag beneath <script src="_framework/blazor.server.js"></script> in the _Host.cshtml file, like this:


<script src="_framework/blazor.server.js"></script>
<script>
window.exampleJsFunctions =
{
ShowMsg: function (message) {
window.alert(message);
}
};
</script>

[#3004] Wednesday, July 29, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dorothylorrainef

Total Points: 456
Total Questions: 102
Total Answers: 115

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
dorothylorrainef questions
;