我是Web开发的新手,我已经搜索了一段时间,但似乎找不到解决方案。我正在asp.net core 2.0中使用剃须刀页面,并且我想根据另一个下拉框的选择填充一个下拉框。当第一个下拉框的值发生更改时,我将以下javascript设置为在剃刀页面中命中一个过程。但是,当我运行代码时,我无法使其正常工作。我认为这是由于我的返回值引起的,但我似乎无法将其作为json值,因为当我尝试这样做时,它总是向我抛出错误。错误是“ JSON在此上下文中无效”。谁能给我建议如何将剃须刀页面中的JSON返回到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);
}
return null;
}
开心每一天1111
牧羊人nacy
相关分类