有无数的例子在www上做这个老年问题。但没有什么能让我超越终点线。这是StudentController上的目标方法:
[HttpGet]
public List<CampusOrganization> GetOrgsByCampusId(int id)
{
var results = repository.GetOrgsByCampusId(id);
return results;
}
这是它返回的一个例子:
[
{
"campusId": 441,
"requireAssociate": true,
"appointments": null,
"organizationId": 362,
"longName": "Alpha Chi Omega"
},
{
"campusId": 441,
"requireAssociate": true,
"appointments": null,
"organizationId": 363,
"longName": "Alpha Gamma Delta"
}, ...
第二个选择(从顶部开始)取决于第一个:
<div class="form-group">
<label asp-for="SelectedCampus.OrganizationId">Campus:</label>
<select asp-for="SelectedCampus.OrganizationId" asp-items="@Model.Campuses" class="form-control" id="Campus">
<option>Select one</option>
</select>
<span asp-validation-for="SelectedCampus.OrganizationId" class="text-danger"></span>
<label asp-for="Student.OrganizationId">Organization:</label>
<select asp-for="Student.OrganizationId" asp-items="@Model.CampusOrganizations" class="form-control" id="Org">
<option>Select one</option>
</select>
<span asp-validation-for="Student.OrganizationId" class="text-danger"></span>
</div>
这是完成且没有错误的Ajax请求:
$(document).ready(function () {
$("#Campus").on("change", function () {
$list = $("#Org");
$.ajax({
url: "/Student/GetOrgsByCampusId",
type: "GET",
data: { id: $("#Campus").val() }, //id of the campus which is used to extract organzitions
traditional: true,
success: function (result) {
$list.empty();
$.each(result, function (i, item) {
$list.append('<option value="' + item["OrganizationId"] + '"> ' + item["LongName"] + ' </option>');
});
},
error: function (xhr, msg) {
alert("readyState: " + xhr.readyState + '\n' + "status: " + xhr.status);
alert("responseText: " + xhr.responseText);;
}
}); //end ajax()
}); //end onchange function
});
最后,这是一个出错的图像。 .each循环在上面的Ajax代码中迭代并完成而没有错误。所以我怀疑,我在循环中构建的内容不正确,