Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda