Им интересно, что случилось с моим сценарием, он не получает значения городов в зависимости от страны, см мой код:AJAX Fill DropDown на основе Dropdown Родитель
Контроллер: отель
private ActionResult FillCity(int countryid)
{
var cities = db.cities.Where(c => c.country_id == countryid);
return Json(cities, JsonRequestBehavior.AllowGet);
}
Jquery/AJAX
<script>
function FillCity() {
var countryid = $('#Country').val();
$.ajax({
url: '@Url.Action("FillCity")',
type: "GET",
dataType: "JSON",
data: { countryid: countryid },
success: function (cities) {
$("#City").html("");
$.each(cities, function (i, city) {
$("#City").append(
$('<option></option>').val(city.id).html(city.name));
});
}
});
}
</script>
Посмотреть
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Country, new SelectList(ViewBag.CountryList, "id", "country_name"), "Select Country", htmlAttributes: new { @class = "form-control", @onchange = "FillCity()" })
@Html.ValidationMessageFor(m => m.Country, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.City, new SelectList(Enumerable.Empty<SelectListItem>(), "id", "name"), "Select City", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
</div>
Я проверяю консоль, кажется, правильный URL
спасибо, теперь его ударил, но не получал никаких значений в выпадающем списке городов. – Nevi
Вы имеете свойства с именем 'id' и' name' (точный случай)? –
Да в моей модели того же случая – Nevi