Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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
Javascript JSON数据未从控制器返回到视图?_Javascript_Json_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript JSON数据未从控制器返回到视图?

Javascript JSON数据未从控制器返回到视图?,javascript,json,ajax,asp.net-mvc,Javascript,Json,Ajax,Asp.net Mvc,我尝试使用JSON将数据从控制器发送到视图。在一个功能中,它工作得很好,但在另一个功能中,它不工作。调试时,我发现数据是从控制器传递的,但不会在视图中返回 这是我的密码 [HttpGet] public ActionResult LoadEmployee(string E_Id) { var _Employee= ApplicationManager.GetEmployee(E_Id); if (_Employee !

我尝试使用JSON将数据从控制器发送到视图。在一个功能中,它工作得很好,但在另一个功能中,它不工作。调试时,我发现数据是从控制器传递的,但不会在视图中返回

这是我的密码

[HttpGet]
        public ActionResult LoadEmployee(string E_Id)
        {
           var _Employee= ApplicationManager.GetEmployee(E_Id);
            if (_Employee != null)
            {
                ViewBag.EmployeeName = _Employee.User != null ? _Employee.User.LoginName : string.Empty;
                ViewBag.Department = _Employee.Department != null ? _Employee.Department : string.Empty;
                ViewBag.JobTitle = _Employee.JobTitle != null ? _Employee.JobTitle : string.Empty;
                ViewBag.RegNo = ReportManager.GetEmployee(E_Id);
                ViewBag.employeeName = ReportManager.GetEmployeeFullName(E_Id);
            }
            var data1 = ViewBag;
            return Json(data1, JsonRequestBehavior.AllowGet);
        }
鉴于:

function Select() {
        var t = $('#tblEmployee').DataTable();
        var rowData = t.rows('.row_selected').data();
        var E_Id = rowData["0"].EmployeeId;

        var EmployeeDetailsIdUrl = '@Url.Action("LoadEmployee")';
             $.ajax({  
                type: "GET",  
                url: EmployeeDetailsIdUrl,  
                contentType: "application/json; charset=utf-8",  
                data: { "E_Id": E_Id },  
                datatype: "json",  
                 success: function (data1) {  
                     $('#modal-info').modal('hide');
                     $('#txtEmployeeId').val(E_Id);
                     $('#txtEmployeeName').val(data1.EmployeeName);
                     $('#txtRegNo').val(data1.RegNo);
                     $('#txtJobTitle').val(data1.JobTitle);
                     $('#txtDepartment').val(data1.Department);

                  },  
                error: function () {  
                    alert("Dynamic content load failed.");  
                }  
            });  
          }
FYR,我添加了我的data1格式,由于公司规则,我隐藏了一些数据


如何将我的数据从控制器发送到视图?Data1在动态视图中,调试时,我找到了它。请帮忙

您在成功方法中收到了整个响应对象,因此我认为您应该

success: function (response) {  
                 $('#modal-info').modal('hide');
                 $('#txtEmployeeId').val(E_Id);
                 $('#txtEmployeeName').val(response.data1.EmployeeName);
                 $('#txtRegNo').val(response.data1.RegNo);
                 $('#txtJobTitle').val(response.data1.JobTitle);
                 $('#txtDepartment').val(response.data1.Department);

您可以尝试使用浏览器中的F12来检查您在UI中实际接收到的内容。

不知何故,我尝试了各种方法,这一方法解决了我的问题,并且对我有效:

[HttpGet]
        public ActionResult LoadEmployeeDetails(string E_Id)
        {

             var _Employee= ApplicationManager.GetEmployee(E_Id);
            var data1= new
            {
                 EmployeeName = _PREmployeeSalary.User != null ? _PREmployeeSalary.User.LoginName : string.Empty,
                Department = _PREmployeeSalary.Department != null ? _PREmployeeSalary.Department : string.Empty,
                JobTitle = _PREmployeeSalary.JobTitle != null ? _PREmployeeSalary.JobTitle : string.Empty,
                RegNo = DailyLeaveReportManager.GetEmployee(E_Id),
                employeeName = DailyLeaveReportManager.GetEmployeeFullName(E_Id)
            };
             return Json(data1, JsonRequestBehavior.AllowGet);
        }

你有什么错误吗?你是否控制了数据1并找到了你期望的数据?什么是
txtEmployeeId
是输入id还是html标记id?不,我没有收到任何错误。以及从控制器发送的预期数据(在动态视图中),但在视图中未收到<代码>变量E_Id=rowData[“0”].EmployeeId我的员工Id是从发送给控制器的弹出模式中选择的Id,使用此Id,我检索data1下的所有其他数据,并尝试在文本框中显示它
txtEmployeeId
和所有以“txt”开头的其他id是我的文本框id..@NijinKoderi我正在将我的数据从viewbag发送到变量data1。。此数据1将返回到视图中。我也尝试过直接发送我的viewbag,但仍然无法进入视图。如果您的data1有值,那么问题可能是您如何设置您的数据您可以提供您的data1值和视图吗code@sayalok请再看一下我的问题,我附上了data1格式的图片。。