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

448
Visualizações
modify content of ASP.NET @HTML.DropDownListFor dinamically on frontend

I'd like to dynamically change my ASP.NET form based on the user's input. First, the user have to select Item1 from a dropdown and based on the selected value, checkItem(); script will enable certain disabled-by-default input fields. I'd like to have something that would change the content of those input fields based on the selected value.

         <tr>
                        <td style="text-align: center;">
                            item1:
                        </td>
                        <td colspan="2">
                            @Html.DropDownListFor(model => model.item1, (SelectList)ViewData["item1List"], htmlAttributes: new { onchange = "checkItem();", id = "Item1", name = "Item1", @class = "form-control", @style = "min-width: 100%!important;", @required = true })
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: center;">
                            item2:
                        </td>
                        <td colspan="2">
                            @if ( # I need something here, or have JavaScript instead #)
                            {
                                @Html.DropDownListFor(model => model.item2, (SelectList)ViewData["item2List"], htmlAttributes: new { id = "item2", name = "item2", disabled = false, @class = "form-control", @style = "min-width: 100%!important;" })
                            }
                            else
                            {
                                @Html.DropDownListFor(model => model.item2, (SelectList)ViewData["differentItem2List"], htmlAttributes: new { id = "item2", name = "item2", disabled = true, @class = "form-control", @style = "min-width: 100%!important;" })
                            }


                        </td>
                    </tr>

for example, if Item1 have 10 options: 5 locomotives and 5 motor trains, if the user selects a locomotive, the checkItem script will enable the item2 input field, that have a list of passenger cars, and if the user selects a motor train (for example a Stadler FLIRT) the item2 will have a list of other motor trains that compatible with the selected one (i.e: other Stadler motor trains).

How can I do something like this, without submitting the form or reloading the whole page?

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

0

You can try to use ajax to get data from action:

$("#Item1").change(function () {
         $.ajax({
                url: "getList",
                type: "GET",
                data: {Item1: $("#Item1").val()},
                success: function (data) {
                    
                    var items = '';
                $("#item2").empty();
                $.each(data, function (i, item) {
                   items += "<option value='" + item.value + "'>" + item.text + "</option>";
                    
                });
                $('#item2').html(items);
                },
                error: function (result) {
                    alert("error occured");
                }
            });

action:

public List<SelectListItem> getList(string Item1)
{
    //return Item2 data 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