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

161
Visualizações
How to access value?

I can't access the variable value when using it in the if-statement if (@Produkt.KategoriID == value). It says "the name 'value' does not exist in current context". How can i make it so I can use the variable in the if-statement?

@if (Model != null)
    {

        <div style="margin-left: 30%; margin-right: 25%;">
            <label> Choose:    </label>
            <select name="Choose" id="select">
                <option value="1">None</option>
                <option value="2">Bronze</option>
                <option value="3" selected>Silver</option>
                <option value="4">Gold</option>
            </select>
        </div>


        <script type="text/javascript">
            var choice = document.getelementbyid('select');
            var value = choice.options[choice.selectedindex].value;
        </script>

        if (@Produkt.KategoriID == value)
        {
        }

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

0

Basically you are trying to reach js variable from razor script. You can not reach it because their life cycles are different. End of the day all C# scripts converted HTML, CSS and js codes and there is no more C# scripts.

Also, for similar situation you can check here:

How to pass a value to razor variable from javascript variable?

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try to use js to add html:

@if (Model != null)
{

    <div style="margin-left: 30%; margin-right: 25%;">
        <label> Choose:    </label>
        <select name="Choose" id="select">
            <option value="1">None</option>
            <option value="2">Bronze</option>
            <option value="3" selected>Silver</option>
            <option value="4">Gold</option>
        </select>
    </div>


    <script type="text/javascript">
        var choice = document.getelementbyid('select');
        var value = choice.options[choice.selectedindex].value;
    </script>
    <div>

    </div>
    <div id="content"></div>


}

js:

$(function () {
            var choice = document.getElementById('select');
            var value = choice.options[choice.selectedindex].value;
            $("#content").innerHTML = "your html here";
            if (1 == value) {
                var content = "";
                content += "your html here";
                document.getElementById("content").innerHTML =content;
            }

        })
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