簡體   English   中英

ASP.Net Core 3.1 Razor Pages 事件處理程序在生產中不起作用

[英]ASP.Net Core 3.1 Razor Pages event handler not working in production

我正在使用 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  

    });
});

任何幫助將不勝感激。

您是否嘗試過像這樣使用 Url.PageLink 方法。

type: "GET",
url: "@Url.PageLink(pageHandler:"GetCitiesList", pageName:"/SectorPlan/Update",values: route values if you have any)",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM