I'm doing a web application in c # asp.net mvc5, I have a dependent dropdown that is loaded through a javascript function and the data is from the database To this dropdown
I want to put a text - Select - , and in turn this text leaves Disabled , but so far I can not get the Disabled to work. p>
Controller
var municQuery = db.n_municipio.Where(c => c.id_provincia == id).ToList();
List<SelectListItem> munic = new List<SelectListItem>();
foreach (var item in municQuery)
{
munic.Add(new SelectListItem()
{
Value = item.id_municipio.ToString(),
Text = item.municipio,
});
}// foreach
munic.Insert(0, new SelectListItem { Value = "0",
Text = "--Seleccione--", Disabled = true });
return Json(munic, JsonRequestBehavior.AllowGet);
The dropdown is loaded without problems no matter how I send it to the view but I do not get Disabled
. When I trace or debug the code, however, I get true
.
The view
JavaScript
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#provincia").change(function () {
$("#id_municipio").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("MunicipioByProvinciaTwo", "Municipio")', // we are calling json method
dataType: 'json',
data: { id: $("#provincia").val() },
success: function (CityMunicipio) {
$.each(CityMunicipio, function (i, CityMunic) {
$("#id_municipio").append('<option value="' + CityMunic.Value + '">' +
CityMunic.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve Municipio.' + ex);
}
});
return false;
})
});
</script>
@Html.DropDownList("id_municipio", new SelectList(string.Empty, "Value", "Text"), "Seleccione un municipio")
I use the same view for the GET and the POST, so in the dropdown below the Select is seen before the submit and then you see the Select which is in the controller
What I can be doing wrong. Thanks in advance.