In a modal bootstrap form I just need to set a given item in a DropDownList already populated, using for example:
$("#MyDropDown").prop('selectedIndex', 1);
I tried using document ready event:
<script type="text/javascript">
$(document).ready(function () {
$("#MyDropDown").prop('selectedIndex', 1);
});
</script>
But it's not working and i assume it's because that thinks I'm talking about the outer View, not the modal. What event can I use for Modal ready?
This is how I create the DropDownList (it's also populated using JQuery)
<select id="MyDropDown" name="ddlMyDropDown" class="form-control"></select>
EDIT: This is how it's populated using Ajax:
function PopulateMyDropDown() {
var procemessage = "<option value='0'>Seleccionar...</option>";
$("#MyDropDown").html(procemessage).show();
var data = JSON.stringify({
idPedidoDisprofarma: idPedido
});
return $.ajax({
url: "/RecepcionDisprofarma/PedidosDisproNoRecibidos",
data: data,
cache: false,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
success: function (data) {
var markup = "";
markup += "<option value='0'>Seleccionar..</option>";;
for (var x = 0; x < data.length; x++) {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
$("#MyDropDown").html(markup).show();
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}