Disabled does not work in the driver

0

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.

    
asked by Mary 18.12.2017 в 22:42
source

1 answer

1

Since finally, the DrowDownList will be rendered in the Html in the following way:

<select id="id_municipio" name="id_municipio">
    <option value="0">--Seleccione--</option>
    <option value="1">Opción 1</option>
    <option value="2">Opción 2</option>
</select>

The solution can be handled with jQuery, just after filling in the data:

$("select option[value='0']").prop('disabled',true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select id="id_municipio" name="id_municipio">
    <option value="0">--Seleccione--</option>
    <option value="1">Opción 1</option>
    <option value="2">Opción 2</option>
</select>

The complete code would look like this:

<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>');
                  });
                  $("select option[value='0']").prop('disabled',true);
            },
            error: function (ex) {
                alert('Failed to retrieve Municipio.' + ex);
            }
        });
        return false;
      })
   });
</script>
    
answered by 19.12.2017 / 16:45
source