Asp.net razor页面 - 如何返回$ .getjson请求的JSON结果? [英] Asp.net razor page - how to return JSON result for $.getjson request?

查看:550
本文介绍了Asp.net razor页面 - 如何返回$ .getjson请求的JSON结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是网络开发的新手,我一直在寻找一段时间,我似乎无法找到解决方案对此。我在asp.net core 2.0中使用剃刀页面,我想根据另一个下拉框选择填充下拉框。当第一个下拉框的值发生变化时,我设置了以下javascript以在我的剃刀页面中点击一个过程。当我运行代码时,我无法让它工作。我认为这是由于我的返回值,但我似乎无法将其作为json值,因为它在我尝试时不断向我抛出错误。错误是JSON在此上下文中无效。任何人都可以向我建议如何将jSON从剃刀页面返回到javascript调用?我试图简单地使用命名空间JSON来返回,但它在ASP.Net Core 2.0中不起作用,因为我收到以下消息Json在当前上下文中不存在。任何帮助将不胜感激!



我尝试过:



以下是我的C#代码:



Hello Everyone,

I am very new to web development and I've been searching around for a while now and I can't seem to find a solution to this. I am using razor pages in asp.net core 2.0 and I want to fill a drop down box based on another drop down box's selection. I set up the below javascript to hit a procedure in my razor page when the value of the first drop down box changes. When I run the code though, I can't get it to work. I think it is due to my return value but I can't seem to get that to be a json value as it keeps throwing an error at me when I try to. The error is that "JSON is not valid in this context". Can anyone suggest to me how to return JSON from razor pages to a javascript call? I tried to simply use the namespace JSON for my return but it doesn't work in ASP.Net Core 2.0 as I get the following message "The name Json does not exist in the current context". Any help would be appreciated!

What I have tried:

Here is my C# code below:

// 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;
   }





以下是视图中的javascript:





Here is the javascript from the view:

@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>

推荐答案

' < span class =code-string>#Department')。change( function (){
var selectedDepartment =
('#Department').change(function () { var selectedDepartment =


#Department)。val();
var cardSelect =
("#Department").val(); var cardSelect =


' #Card');
cardSelect.empty();
if (selectedDepartment!= null && selectedDepartment!= ' '){
('#Card'); cardSelect.empty(); if (selectedDepartment != null && selectedDepartment != '') {


这篇关于Asp.net razor页面 - 如何返回$ .getjson请求的JSON结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆