我正在使用 ASP.NET 3.1 Core Razor Pages 并嘗試在 onchange 事件時為我的下拉串列添加實時互動。
我有兩個下拉串列,第一個填充省份,第二個將使用 onchange 事件填充所選省份的城市。

在開發程序中一切順利,但如果我發布我的專案,城市下拉串列不會發生實時互動,我會收到此錯誤
GET http://localhost/SectorPlan/Update?handler=GetCitiesList&GovernorateId=15 404(未找到)
這是我的代碼。
更新.cshtml.cs
public JsonResult OnGetGetCitiesList(int GovernorateId)
{
var cities = manageCities.GetCities().Where(c => c.GovId == GovernorateId).ToList();
return new JsonResult(cities);
}
更新.cshtml
<div class="form-group">
<label asp-for="Governarate.Id">Governorate Name</label>
<select class="form-control governarateDropdown" asp-for="Governarate.Id" asp-items="@(new SelectList(Model.Governarates,"Id", "Name"))">
<option selected disabled>Select Governorate</option>
</select>
<span asp-validation-for="Governarate.Id" class="alert-danger"></span>
</div>
<div class="form-group">
<label asp-for="City.Id">City Name</label>
<select class="form-control cityDropdown" asp-for="City.Id">
<option selected disabled>Select City</option>
</select>
<span asp-validation-for="City.Id" class="alert-danger"></span>
</div>
JS代碼
//Bind City dropdownlist
$(".governarateDropdown").change(function () {
var govId = $(this).val();
console.log(govId);
$.ajax({
type: "GET",
url: '/SectorPlan/Update?handler=GetCitiesList',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $.param({ GovernorateId: govId }),
success: function (result) {
//alert(JSON.stringify(result));
var item = "";
$(".cityDropdown").find('option').not(':first').remove();
item = '<option selected disabled value="">Select City</option>'
$.each(result, function (i, city) {
console.log(city);
item = '<option value="' city.id '">' city.name '</option>'
});
$(".cityDropdown").html(item);
}, //End of AJAX Success function
failure: function (result) {
//alert(result.responseText);
console.log(result.responseText);
}, //End of AJAX failure function
error: function (result) {
//alert(result.responseText);
console.log(result.responseText);
} //End of AJAX error function
});
});
任何幫助將不勝感激。
uj5u.com熱心網友回復:
您是否嘗試過像這樣使用 Url.PageLink 方法。
type: "GET",
url: "@Url.PageLink(pageHandler:"GetCitiesList", pageName:"/SectorPlan/Update",values: route values if you have any)",
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485135.html
