gpt4 book ai didi

javascript - ASP.Net Razor Pages - 如何将 json 从 razor 页面返回到 javascript $.getJson?

转载 作者:行者123 更新时间:2023-12-05 00:37:49 24 4
gpt4 key购买 nike

我对 Web 开发非常陌生,我已经搜索了一段时间,但似乎找不到解决方案。我在 asp.net core 2.0 中使用 Razor 页面,我想根据另一个下拉框的选择填充一个下拉框。当第一个下拉框的值发生变化时,我设置了以下 javascript 以在我的 Razor 页面中点击一个过程。但是,当我运行代码时,我无法让它工作。我认为这是由于我的返回值,但我似乎无法将其设为 json 值,因为当我尝试这样做时它不断向我抛出错误。错误是“JSON 在此上下文中无效”。谁能向我建议如何将 JSON 从 Razor 页面返回到 javascript 调用?

任何帮助,将不胜感激!

    @section Scripts {
<script type="text/javascript">
$('#Department').change(function () {
var selectedDepartment = $("#Department").val();
var cardSelect = $('#Card');
cardSelect.empty();
if (selectedDepartment != null && selectedDepartment != '') {
$.getJSON('@Url.Action("GetCardsByDivisionAndStatus")', { divisionID: selectedDepartment }, function (cards) {
if (cards != null && !jQuery.isEmptyObject(cards)) {
cardSelect.append($('<option/>', {
Card_ID: null,
Card_Number: ""

}))
$.each(cards, function (index, card) {
cardSelect.append($('<option/>', {
Card_ID: card.Card_ID,
Card_Number: card.Card_Number
}));

});

};

});
}
});
</script>

}

这是我的 C# 代码(我尝试使用 JsonResult 但这也不起作用):
           // If the user selects a division then make sure to get the cards for that division only
[HttpGet]
public ActionResult GetCardsByDivisionAndStatus(string divisionID)
{
int checkinStatus;
int intdivisionID;

if (divisionID != "0" && divisionID != null)
{
// Retrieve a status of checked in so that only cards with a checked in status can
// be checked out.
checkinStatus = linqQuery.GetCardStatusByStatusDesc("Checked In", _Context);

intdivisionID = Convert.ToInt32(divisionID);

// Retrieve list of cards that have the checkin status ID
CardList = linqQuery.GetCardListByStatusIDandDeptID(checkinStatus, intdivisionID, _Context);

// Create the drop down list to be used on the screen.
carddropdown = new List<CardDropDown>();
carddropdown = loaddropdowns.ReturnDropDownList(CardList);
return new JsonResult(CardList);
}

return null;
}

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ----------

所以我改变了代码如下,现在我得到一个解析错误“JSON.parse:JSON数据的第1行第1列的意外字符”我无法弄清楚发生了什么,因为我看不到什么数据回来了,它无法解析。我下面的代码是否没有转换为 JSON,如果没有,我错过了什么?
    @section Scripts {
<script type="text/javascript">
$('#Department').change(function () {
var selectedDepartment = $("#Department").val();
var cardSelect = $('#Card');
cardSelect.empty();
if (selectedDepartment != null && selectedDepartment != '') {
$.getJSON('@Url.Action("/CheckOutCard?handler=CardsByDivisionAndStatus")', { divisionID: selectedDepartment }, function (cards) {
if (cards != null && !jQuery.isEmptyObject(cards)) {
cardSelect.append($('<option/>', {
Card_ID: null,
Card_Number: ""

}))
$.each(cards, function (index, card) {
cardSelect.append($('<option/>', {
Card_ID: card.Card_ID,
Card_Number: card.Card_Number
}));

});

};

});
}
});
</script>

这是我更新的过程的 C# 代码:
            // If the user selects a division then make sure to get the cards for that division only
[HttpGet]
public JsonResult OnGetCardsByDivisionAndStatus(string divisionID)
{
int checkinStatus;
int intdivisionID;

if (divisionID != "0" && divisionID != null)
{
// Retrieve a status of checked in so that only cards with a checked in status can
// be checked out.
checkinStatus = linqQuery.GetCardStatusByStatusDesc("Checked In", _Context);

intdivisionID = Convert.ToInt32(divisionID);

// Retrieve list of cards that have the checkin status ID
CardList = linqQuery.GetCardListByStatusIDandDeptID(checkinStatus, intdivisionID, _Context);

// Create the drop down list to be used on the screen.
carddropdown = new List<CardDropDown>();
carddropdown = loaddropdowns.ReturnDropDownList(CardList);
var converted = JsonConvert.SerializeObject(carddropdown);
return new JsonResult(converted);
}

return null;
}

最佳答案

将您的方法重命名为 获取 CardsByDivisionAndStatus (注意“OnGet”前缀)并在 jquery 代码中将 url 更改为

$.getJSON('/{PageRoute}?handler=CardsByDivisionAndStatus'
e.g.
$.getJSON('/About?handler=CardsByDivisionAndStatus'

请注意,处理程序查询字符串参数名称将是您的方法名称,没有 获取 字首。

关于javascript - ASP.Net Razor Pages - 如何将 json 从 razor 页面返回到 javascript $.getJson?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com