Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Why razor executes a C# method inside a button click method on compilation without clicking the button?

-ASP.NET 4.7 Web Application-

I hold some notifications in the session to show the user. I want to remove the session variable on button click.

The idea is to make sure the user gets notified so as long as the user don't hit "Got it" button, I won't be removing the notifications from the session so that every time the users reload the page they will see the same notification window.

I have a button in a modal as follows:

<button type="button" class="btn btn-secondary" onclick="clearSession()" data-dismiss="modal">Got it</button>

And this is the method supposed to be called on button click:

<script type="text/javascript">
    function clearSession() {
        @{Session.Remove("WhatsNewList");}
    };
</script>

The problem is that the following part of the code precompiles and executes even though the button is not clicked.

@{Session.Remove("Notifications");}

I want to know what is the reason for this and is there a way to prevent this.

I know I can call a C# method using Ajax or jQuery but I want to know if there is a way I can do it this way as well.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Sourounding an expression Using @{ } will cause the expression to be evaluated server side before the page is loaded into the browser meaning the code will execute as soon as the page is requested. So, the short answer is, you cannot do what you are trying to do using this approach.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda