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

254
Visualizações
JS+ASP Net Core Get data from controller

the question arose: How to get data from the controller in asp net core using js? I tried to send a post request head-on, but it seems to me there is a more elegant solution At the same time, I need this to happen when the html of the Select element changes I wrote a script that changes the field when the select changes

    function Change_Aud() {
    var x = document.getElementById("Levelid").value;
    document.getElementById("Hardware_La").value = x ;
   
}

Controller example

        public async Task<IActionResult> Get_Hardware_Unit(int id)
    {
        Level level  = _context.Levels.Where(p => p.Number == id.ToString()).First();
        
        return Content(level.Public_Hardware.ToString());
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Test page code in cshtml:

@{
    ViewData["Title"] = "Home Page";
    int count = 5;
}
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>

<script>
    function myFunction(selectObject) {
        var value = selectObject.value;
        $("#selectnum").html(value);
        backendfunc(value);
    }
    function backendfunc(value) {
        $.ajax({
            url: "/Test/Get_Hardware_Unit",
            type: "GET",
            data: { id: value },
            success: function (res) {
                $("#convertnum").html(res);
            },
            error: function (hata, ajaxoptions, throwerror) {
                $("#convertnum").html("convert failed");
            }
        });
    }
</script>

<div>
    <select name="num" id="num-select" onchange="myFunction(this)">
        <option value="">--Please choose an option--</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</div>
<br />
<br />
<div>
    You select value is : <span id="selectnum"></span>
</div>
<br />
<br />
<div>
    After backend converted, here is : <span id="convertnum"></span>
</div>

My function in Controller:

public async Task<IActionResult> Get_Hardware_Unit(int id) {
    string result = string.Empty;
    if (id == 1) {
        result = "①";
    }
    if (id == 2)
    {
        result = "②";
    }
    if (id == 3)
    {
        result = "③";
    }
    return Content(result);            
}

Test Result

enter image description here

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