Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用json和javascript从控制器获取两个要查看的对象列表_C#_Jquery_Asp.net Mvc_Json - Fatal编程技术网

C# 使用json和javascript从控制器获取两个要查看的对象列表

C# 使用json和javascript从控制器获取两个要查看的对象列表,c#,jquery,asp.net-mvc,json,C#,Jquery,Asp.net Mvc,Json,我在视图中有一个下拉列表,在进行更改时,我需要通过javascript代码调用控制器中的方法,此方法必须使用JsonResult返回两个对象列表,并在javascript中处理视图中的结果 以下是我的看法: Files: @Html.DropDownList("File", Enumerable.Empty<SelectListItem>()) <table id="trTable" class="table table-condensed table-hover

我在视图中有一个下拉列表,在进行更改时,我需要通过javascript代码调用控制器中的方法,此方法必须使用JsonResult返回两个对象列表,并在javascript中处理视图中的结果

以下是我的看法:

Files:

    @Html.DropDownList("File", Enumerable.Empty<SelectListItem>())

<table id="trTable" class="table table-condensed table-hover table-bordered">
</table>
那么如何解决这个问题呢??
我需要您的帮助

您可以返回合成对象:

var result=new { PWs=PWs, Files=Files};
return Json(result, JsonRequestBehavior.AllowGet);

以下是解决方案,控制器代码如下:

 Class ClassDetails = db.Classes.Find(clasId);
 // finding the branch of the student for registration and other fees
 int branchId = db.Students.Where(z => z.StudentID == id).Select(z => z.BranchID).FirstOrDefault();
 BranchSetting branchSettings = db.BranchSettings.Where(z => z.BranchID == branchId).FirstOrDefault();
 return Json(new { ClassDetails,branchSettings}, JsonRequestBehavior.AllowGet);
下面是jquery成功回调的代码

 success:
        function (result) {
            console.log(result);
            var classDetails = result.ClassDetails;
            var branchFee = result.branchSettings;
            if (classDetails == null || branchFee == null) {
                $("#txtMsg").html("Please contact admin.");
                $("#txtMsg").show();
                $("#Things").hide();
                $("#myModal").modal('show');

            }
            else {
                $("#txtClassName").html(classDetails.ClassName);
                $("#txtTutionFee").html(classDetails.TutionFee);
                $("#txtComputerFee").html(classDetails.ComputerFee);
                $("#txtTotalMonthlyFee").html(classDetails.TotalFee);
                $("#txtRegistrationFee").html(branchFee.RegistrationFee);
                $("#txtAddmissionFee").html(branchFee.AddmissionFee);
                $("#txtSecurityFee").html(branchFee.SecurityFee);

                $("#myModal").modal('show');
            }
        },

在viewmodel中包装您的2个列表,并序列化如何在javascript中使用结果?您可以对这两个列表使用结果[0]和结果[1]。如果您从jQuery ajax获得
数据
对象,则只需调用
数据.PWs
数据.Files
(我不确定大小写,可能是data.PWs和data.Files)
 Class ClassDetails = db.Classes.Find(clasId);
 // finding the branch of the student for registration and other fees
 int branchId = db.Students.Where(z => z.StudentID == id).Select(z => z.BranchID).FirstOrDefault();
 BranchSetting branchSettings = db.BranchSettings.Where(z => z.BranchID == branchId).FirstOrDefault();
 return Json(new { ClassDetails,branchSettings}, JsonRequestBehavior.AllowGet);
 success:
        function (result) {
            console.log(result);
            var classDetails = result.ClassDetails;
            var branchFee = result.branchSettings;
            if (classDetails == null || branchFee == null) {
                $("#txtMsg").html("Please contact admin.");
                $("#txtMsg").show();
                $("#Things").hide();
                $("#myModal").modal('show');

            }
            else {
                $("#txtClassName").html(classDetails.ClassName);
                $("#txtTutionFee").html(classDetails.TutionFee);
                $("#txtComputerFee").html(classDetails.ComputerFee);
                $("#txtTotalMonthlyFee").html(classDetails.TotalFee);
                $("#txtRegistrationFee").html(branchFee.RegistrationFee);
                $("#txtAddmissionFee").html(branchFee.AddmissionFee);
                $("#txtSecurityFee").html(branchFee.SecurityFee);

                $("#myModal").modal('show');
            }
        },