Fill DropDown from the selection of another DropDown [duplicate]

1

I'm trying to fill a DropDown from the selection of another DropDown , basically, when the user selects a state in a DropDown , the cities of this state must be displayed in another DropDown , dynamically .

I have the following code:

VIEW

<div class="col-lg-2">
    <div class="form-group">
        <label class="control-label">
            Departamento:
        </label>
        <div id="EstadoDifBrasil">                                
            @this.AutoSelect(x => x.StatePar).Class("form-control required").FirstOption("", "Selecione uma opção")                                
        </div>
    </div>
</div>
<div class="col-lg-3">
    <div class="form-group">
        <label class="control-label">
            Cidade:
        </label>
        <div id="CidadeDifBrasil">
            @this.AutoSelect(x => x.CityPar).Class("form-control required").FirstOption("", "Selecione uma opção")
        </div>
    </div>
</div>

$("#StatePar").change(function () {
    var idStatePar = $("#StatePar").val();
    $.post("@Url.Action(MVC.Escritorio.Clientes.VerificarCidadeDifBr())", { codCitySel: idStatePar })
        .success(function (result) {
            alert(result.length);
        })
        .error(function (ex) {
            alert('Erro ' + ex);
        })
});

CONTROLLER

[HttpGet]
public virtual JsonResult VerificarCidadeDifBr(int codCitySel)
{
    var xpto = new List<TCity>();
    xpto = TCity.ListAllCities().Where(x => x.State.Id == codCitySel).OrderBy(x => x.Name).ToList(); //.ToSelectList(x => x.Id, x => x.Name);

    return Json(xpto, JsonRequestBehavior.AllowGet);
}

No controller I can list the cities of the state that I select, but I can not return to the view via Json , just alert "Erro: [object Object]" .

Does anyone have any light, please?

Thanks and great week!

    
asked by anonymous 18.10.2016 / 15:35

1 answer

0

I think you've complicated a lot. You could have written the Ajax event like this:

@section Scripts 
{
    <script>
        $("#StatePar").change(function () {
            $.ajax({
                url: "/Departamentos/CidadesPorDepartamento/" + id,
                success: function (data) {
                    $("#CityPar").empty();
                    $("#CityPar").append('<option value>Selecione...</option>');
                    $.each(data, function (index, element) {
                        $("#CityPar").append('<option value="' + element.CityId + '">' + element.Name + '</option>');
                    });
                }
            });
        });
    </script>
}

Action in Controller :

[HttpGet]
public virtual JsonResult CidadesPorDepartamento(int id)
{
    var xpto = TCity.ListAllCities()
                    .Where(x => x.State.Id == codCitySel)
                    .OrderBy(x => x.Name)
                    .Select(x => new { CityId = x.Id, Name = x.Name })
                    .ToList(); //.ToSelectList(x => x.Id, x => x.Name);

    return Json(xpto, JsonRequestBehavior.AllowGet);
}
    
16.02.2017 / 18:31