首页 > 解决方案 > 无法使用 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);
        }

在此处输入图像描述

标签: jqueryasp.net-corerazor

解决方案


我刚刚在 startup.cs 文件中注释了 AddJsonOptions 行,整个应用程序选择项目恢复正常运行

services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

                 //.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

推荐阅读