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

87
Visualizações
MVC 5 Option Empty Line With JavaScript
$('#DepartmentCode').change(function () {
    var empName = $('#Employee_No');
    var department = $('#DepartmentCode').val();
    empName.empty();

    $.ajax({
        url: "/LeaveRequestEntry/GetEmployeesByDept",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({ ClassCode: department }),
        success: function (data) {
            $.each(data, function (index, option) {
                empName.append('<option value=' + option.Value + '>' + option.Text + '</option>');
            });
        }
    });
});

This is my JavaScript code.

   ViewBag.Employee = employeeRepository.GetAllByDepartment(ClassCode).Select(e => new SelectListItem { Text = e.Full_Name + "_" + e.No, Value = e.No });
    return Json(new SelectList(ViewBag.Employee, "Value", "Text"));

This is my controller part.

    <div class="field">
        @Html.DropDownList("Department", new SelectList(ViewBag.DepartmentList, "Value", "Text"), LeaveRequestEntryRes.SelectableMessage, new { @class = "ui fluid dropdown", @id = "DepartmentCode" })
    </div>
    <div class="field">
        @Html.DropDownList("Employee_No", new SelectList(ViewBag.EmployeeList, "Value", "Text"), LeaveRequestEntryRes.SelectableMessage, new { @class = "ui fluid dropdown" })
    </div>

There are 2 options in the html part. When I select a department, the employees of that department come. My problem is that one of them is always selected. I want to click and select. how do i fix it so that if i click it ?? Sorry for my English. :)

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

0

Could you not just set a placeholder option above the loop in the success?

for example:

empName.append('<option value="" selected>Select an option</option>');

// The loop to populate
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