jquery - 无法使用 jQuery 填充项目
问题描述
选择国家时出现空白列表,代码之前可以使用但不知道发生了什么变化,当前视图如下所示,并带有错误消息
<div class="form-group">
<label asp-for="UniversityId" class="control-label"></label>
<select id="uniid" asp-for="UniversityId" class="form-control" asp-items="@(new SelectList(Enumerable.Empty<SelectListItem>(), "Id", "EnUniversityName"))">
<option disabled selected value="">@Localizer["SelectUniversity"]</option>
</select>
<span asp-validation-for="UniversityId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FacultyId" class="control-label"></label>
<select id="facid" asp-for="FacultyId" class="form-control"
asp-items="@(new SelectList(Enumerable.Empty<SelectListItem>(), "Id", "ArFacultyName"))">
<option value="">@Localizer["SelectFaculty"]</option>
</select>
<span asp-validation-for="FacultyId" class="text-danger"></span>
</div>
<script>
$(document).ready(function () {
$("#cntid").change(function () {
$("#uniid").empty();
$("#uniid").append($('<option>', { text: "Select Univercity" }));
$("#facid").empty();
$("#facid").append($('<option>', { text: "Select Faculty" }));
if ($("#cntid").val() > 0) {
var CountryOptions = {};
CountryOptions.url = "/@CultureInfo.CurrentCulture.Name/EducationalLevels/GetUniversitiesList/";
CountryOptions.data = { countryid: $("#cntid").val() };
CountryOptions.success = function (data) {
$.each(data, function (index, row) {
$("#uniid").append($('<option>', {value: row.value, text: row.text}))
});
};
CountryOptions.error = function () { alert("Error: can't retrieve the list of universities!!!!"); };
$.ajax(CountryOptions);
}
});
});
</script>
而控制器代码是
public JsonResult GetUniversitiesList(int countryid)
{
var universities = new SelectList(_context.Universities.Where(u => u.CountryId == countryid), "Id", "ArUniversityName");
return Json(universities);
}
解决方案
我刚刚在 startup.cs 文件中注释了 AddJsonOptions 行,整个应用程序选择项目恢复正常运行
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
//.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
推荐阅读
- c# - 如何从同一 Azure AD 中的另一个 Web 应用程序访问 Web api?
- python - 使用切片和列表索引数据框
- c - 在 Ubuntu 18.04.1 上与 gcc-6 的覆盖兼容性?
- r - r 使用随机换行符格式化 csv 并导出
- node.js - 不从身份验证代码返回刷新令牌
- jquery - 通过 jQuery 重新加载脚本
- html - 如何强制固定导航栏具有页面的全宽而不是视口?
- jquery - jQuery toggleClass 显示面板。通过单击身体或屏幕上的任何位置关闭
- c - 根据字符串输入返回 sizeof
- c# - 单击文本框时在 C# WPF 中设置插入符号/光标位置