I have two dropdowns one for the clientes and one for the contacts of the cliente. When the page is loaded I want the primary contact of the cliente to be selected or when the user selected a diffrent cliente. Here is the html that is generated.
<select id="drContacto" name="drContacto" class="form-control valid" aria-invalid="false">
<option value="39">Remi</option>
<option value="40">Bob</option>
<option value="56">Marck</option>
<option value="71">Luck</option>
<option value="122">Zoe</option>
<option value="123">roberto</option>
<option value="124">jason</option>
</select>
This is the jquery I have writen.
<script type="text/javascript">
$(document).ready(function () {
var clienteID = $('#drClient').val();
$.ajax({
url: '/ordenderecibo/FillContactos',
type: "GET",
dataType: "JSON",
data: { ID: clienteID },
success: function (cities) {
$("#drContacto").html(""); // clear before appending new list
$.each(cities, function (i, contactos) {
$("#drContacto").append(
$('<option></option>').val(contactos.contacto_id).html(contactos.nombre_contacto));
});
$("#drContacto option[value=" + @Model.contrato.contacto_id + "]").prop("selected", true);
}
});
});
function FillContactos() {
var clienteID = $('#drClient').val();
$.ajax({
url: '/ordenderecibo/FillContactos',
type: "GET",
dataType: "JSON",
data: { ID: clienteID },
success: function (cities) {
$("#drContacto").html(""); // clear before appending new list
$.each(cities, function (i, contactos) {
$("#drContacto").append(
$('<option></option>').val(contactos.contacto_id).html(contactos.nombre_contacto));
});
$("#drContacto option[value=" + @Model.contrato.contacto_id + "]").prop("selected", true);
}
});
}
</script>
EXAMPLE
Lets say that
@Model.contrato.contacto_id = 56
I would want it to be selected like this
<select id="drContacto" name="drContacto" class="form-control valid" aria-invalid="false">
<option value="39">Remi</option>
<option value="40">Bob</option>
<option value="56" selected="selected">Marck</option>
<option value="71">Luck</option>
<option value="122">Zoe</option>
<option value="123">roberto</option>
<option value="124">jason</option>
</select>
However this is NOT the case and I don't understand why. Any help would be greatly appreciated. Thank you.