我目前在尝试填充级联选择列表时收到错误,该列表的 javascript 函数是由上一个列表的 on 更改触发的,但是每次控制器将 json 对象返回给 Ajax 回调时,Google Chrome DevTools 都会抛出错误:
DevTools was from the page
Once page is reloaded, DevTools will automatically reconnect
我试图启用日志并使用标志运行 Chrome,但是没有记录任何内容,也没有任何内容保存在“网络”选项卡的日志中。
我已经通过向第一个下拉列表添加提交按钮并以这种方式调用/填充第二个下拉列表来测试填充选择标签,这似乎工作正常。是否有任何理由使用onchangeonHtml.DropDownListFor()会导致 DevTools 抛出此错误?
包含代码的 PartialView(我添加了我使用的两种技术):
<div class="form-group">
<label>Select Country</label>
<!-- This works in filling the second dropdownlist. -->
@Html.DropDownListFor(m => m.SelectedCountry, new SelectList(Model.Country, "FieldID", "Value"), new { @id = "_ddlCountries", @class = "fr-select" })
<button type="submit" onclick="javascript: GetCities()">Test</button>
<!-- ********************************************** -->
<!-- This does not work in filling the second dropdown -->
@Html.DropDownListFor(m => m.SelectedCountry, new SelectList(Model.Country, "FieldID", "Value"), new { @id = "_ddlCountries", @class = "fr-select", onchange="javascript: GetCities()" })
<!-- ********************************************** -->
</div>
<div class="form-group">
<label>Select City</label>
<select class="fr-select" id="_cities"></select>
</div>
两者都进入下面的 javascript 函数,但onchange抛出 DevTools 消息/错误:
function GetCities() {
debugger;
var ddlText = $('#_ddlCountries option:selected').text();
$.ajax({
url: '@Url.Action("GetCities", "Home")',
type: 'POST',
data: JSON.stringify(ddlText),
contentType: 'Application/json',
success: function (result) {
debugger;
//Create a markup for a select
var markup = "";
//Populate the markup
for (var i = 0; i < result.length; i++) {
markup += "<option Value=" + result[i].FieldID + ">" + result[i].Value + "</option>";
}
//Populate dropdown with value
$("#_cities").html(markup).show();
}
})
}
控制器动作:
public ActionResult GetCities([FromBody] string ddlText)
{
List<FieldMap> cities = new List<FieldMap>();
cities.Add(new FieldMap
{
FieldID = 0,
Value = "-- Please Select a City --"
});
foreach (var city in _uploadedFileModel.cities.Where(x => x.name == ddlText).OrderBy(x => x.city.name))
{
cities.Add(new FieldMap
{
FieldID = cities.Select(x => x.FieldID).Max() + 1,
Value = city.name
});
}
return Json(cities);
}
这适用于 MVC 框架,但由于某种原因它不适用于 Core。有什么原因onchange会触发此错误吗?