Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Blazor return bool from JS

I need to send from JS to .NET bool type when clicking on any part of the site, except for necessary classes, so that the div is closed inside @if statement.

I copied some information from here How do I call a C# method with parameters from JavaScript in Blazor WebAssembly?, but he didn't help much with my problem due to insufficient experience with JSInterop.

In C# script, menu opens without difficulties, only issues went with JS. That's why I need help. (Of course, I can remove @if and make closing and opening assignment all classes, but it seems to me this is a "crutch")

Thanks for answers!

HTML:

<button @onclick="ToggleMenu">
@if(!_collapseNavMenu)
{
Something
}

C#:

public static bool _collapseNavMenu { get; set; }

private void ToggleMenu()
{
    _collapseNavMenu = !_collapseNavMenu;
}


[JSInvokable]
public static void CheckBool(bool check)
{
    _collapseNavMenu = check;
}

private async Task OnBtn()
{
    await js.InvokeAsync<object>("TestJs");
}

JS:

var jsCheck = true;

window.onclick = function(event) {
var dropdowns = document.getElementsByClassName("header__buttons");
var i;
if (!event.target.matches('.js-close')) {
        if (jsCheck === true) {
            function TestJs() {
                var check = true;
                DotNet.invokeMethod('Heaven', 'CheckBool', check);
            }
            TestJs();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!