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!