Folgendes ist meine cascade Dropdown-Liste Abfrage. Länder listen auf, aber nicht die Staaten, die in meiner Dropdown-Liste geladen werden. Wenn mir jemand helfen kann, die Anfrage zu korrigieren, bitte.MVC-Controller Abfrage
public ActionResult CountryList()
{
var countries = db.Countries.OrderBy(x=>x.CountryName).ToList();
// IQueryable countries = Country.GetCountries();
if (HttpContext.Request.IsAjaxRequest())
{
return Json(new SelectList(
countries,
"CountryID",
"CountryName"), JsonRequestBehavior.AllowGet
);
}
return View(countries);
}
public ActionResult StateList(int CountryID)
{
IQueryable <State> states= db.States. Where(x => x.CountryID == CountryID);
if (HttpContext.Request.IsAjaxRequest())
return Json(new SelectList(
states,
"StateID",
"StateName"), JsonRequestBehavior.AllowGet
);
return View(states);
}
finden Sie die View-Datei Java-Script auch containg:
@section scripts {
<script type="text/javascript">
$(function() {
$.getJSON("/Dropdown/Countries/List",function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, country) {
items += "<option value='" + country.Value + "'>" + country.Text + "</option>";
});
$("#Countries").html(items);
});
$("#Countries").change(function() {
$.getJSON("/Dropdown/States/List/" + $("#Countries > option:selected").attr("value"), function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$("#States").html(items);
});
});
});
</script>
}
<h1>@ViewBag.Title</h1>
@using (Html.BeginForm())
{
<label for="Countries">Countries</label>
<select id="Countries" name="Countries"></select>
<br /><br />
<label for="States">States</label>
<select id="States" name="States"></select>
<br /><br />
<input type="submit" value="Submit" />
}
Können Sie auch Ihren JavaScript-Code, die Ajax-Aufrufe ausführen? – serhiyb
Rufen Sie die .ToList() - Methode für Ihr Statusobjekt auf. –
IQueryable states = db.States. Wobei (x => x.LandID == CountryID) .ToList(); ...... Dieser Code zeigt die rote Linie –