ASP.Net剃刀页面-如何将JSON从剃刀页面返回到javascript $ .getJson?

我是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;

    } 


慕哥6287543
浏览 162回答 2
2回答

开心每一天1111

所以我找出了问题所在。显然,我的代码中不需要@ URL.Action。这导致我没有点击我的C#代码,这反过来导致了对我的电话的空响应。我已经将我的javascript代码修改为如下所示,以显示我在说什么。感谢Mohsin尝试帮助我。@section Scripts {<script type="text/javascript">&nbsp; &nbsp; $('#Department').change(function ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var selectedDepartment = $("#Department").val();&nbsp; &nbsp; &nbsp; &nbsp; var cardSelect = $('#Card');&nbsp; &nbsp; &nbsp; &nbsp; cardSelect.empty();&nbsp; &nbsp; &nbsp; &nbsp; if (selectedDepartment != null && selectedDepartment != '')&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.getJSON("/CheckOutCard?handler=CardsByDivisionAndStatus", { divisionID: selectedDepartment }, function (cards)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(cards, function (index, card)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardSelect.append($('<option/>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: card.card_ID,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: card.card_Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</script> }

牧羊人nacy

将您的方法重命名为OnGet CardsByDivisionAndStatus(请注意“ OnGet”前缀),然后在jquery代码中将url更改为$.getJSON('/{PageRoute}?handler=CardsByDivisionAndStatus'e.g.$.getJSON('/About?handler=CardsByDivisionAndStatus'请注意,处理程序querystring参数名称将是您的方法名称,不带OnGet前缀。
打开App,查看更多内容
随时随地看视频慕课网APP