无法使用jQuery填充项目

问题描述 投票:-1回答:1

我在选择国家时得到空白列表,代码以前工作但不知道发生了什么变化,当前视图如下所示,错误消息

enter image description here

 <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);
        }

enter image description here

jquery asp.net-core razor
1个回答
0
投票

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

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

                 //.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
© www.soinside.com 2019 - 2024. All rights reserved.