0
I have this HTML, and would like when the user selects the state, it loads the city data of the selected state:
<label asp-for="Cidade" class="col-md-1 control-label" style="text-align:left;"></label>
<div class="col-md-3">
<select asp-for="Cidade" asp-items="@Model.CodigoMunicipioList" id="cbmunicipio" class="form-control"> <option disabled selected>Selecione o Município</option></select>
<span asp-validation-for="Cidade" class="text-danger"></span>
</div>
<label asp-for="UF" class="col-md-1 control-label" style="text-align:left;"></label>
<div class="col-md-2">
<select asp-for="UF" class="form-control">
<option disabled selected>Selecione</option>
<option value="AC">AC</option>
<option value="AL">AL</option>
<option value="AM">AM</option>
<option value="AP">AP</option>
<option value="BA">BA</option>
<option value="CE">CE</option>
<option value="DF">DF</option>
<option value="ES">ES</option>
<option value="GO">GO</option>
<option value="MA">MA</option>
<option value="MG">MG</option>
<option value="MS">MS</option>
<option value="MT">MT</option>
<option value="PA">PA</option>
<option value="PB">PB</option>
<option value="PE">PE</option>
<option value="PI">PI</option>
<option value="PR">PR</option>
<option value="RJ">RJ</option>
<option value="RN">RN</option>
<option value="RS">RS</option>
<option value="RO">RO</option>
<option value="RR">RR</option>
<option value="SC">SC</option>
<option value="SE">SE</option>
<option value="SP">SP</option>
<option value="TO">TO</option>
</select>
<span asp-validation-for="UF" class="text-danger"></span>
</div>
And here’s how I load the data:
var cidade = db.MunicipioIBGE.OrderBy(c => c.NomeMunicipio).Select(y => new { Id = y.CodigoIBGE, Value = y.NomeMunicipio });
model.CodigoMunicipioList = new SelectList(cidade, "Id", "Value");
Only it won’t filter.
This is the function where the where
[HttpGet]
public ActionResult BuscaCidades(string estado)
{
var cidade = db.MunicipioIBGE.OrderBy(c => c.NomeMunicipio).Where(c => c.Estado == estado).Select(y => new { Id = y.CodigoIBGE, Value = y.NomeMunicipio });
return Json(new { cidade });
}
And here the function CarregaCidade
:
function CarregaCidade(estado) {
//var estado = $("#UF").val();
$.ajax({
type: 'GET',
data: estado,
url: '/Empresas/BuscaCidades',
dataType: 'json',
success: function (dados) {
if (dados !== null) {
var selectbox = $('#cbmunicipio');
$('<option>').val("0").text("Selecione um município").appendTo(selectbox);
$.each(dados, function (i, d) {
$('<option>').val(d.Id).text(d.value).appendTo(selectbox);
})
}
},
error: function () {
console.log("Falha de enviar AJAX");
}
})
}
are you sure the request is coming, @marianac_costa? I mean, some request is being sent to where does this "var company" your?
– gabrielfalieri
@gabrielfalieri I haven’t done it in ajax yet, because I don’t know how to do, so I need the user to select the state, he selects the city, and there’s just how I carry all, now I need to filter by state.
– Mariana
I’ll put the answer there for you :)
– gabrielfalieri
@gabrielfalieri thank you
– Mariana
Look here, let me know
– gabrielfalieri
@gabrielfalieri edited the question, so you can understand what is happening.
– Mariana
Let’s go continue this discussion in chat.
– gabrielfalieri