【问题标题】:Ajax error function is firing on json request from MVC controllerAjax 错误函数在来自 MVC 控制器的 json 请求时触发
【发布时间】:2017-01-30 04:21:02
【问题描述】:

大家好!我正在使用 ajax 发送和检索数据,我实际上是在向 MVC 控制器发送数据,根据该控制器,我可以根据加入 var 类型变量从两个表中获取数据,但问题是当我从对 ajax 的查询,它会触发错误函数而不是成功。this is my ajax code

this is my controller (note: i am using view model for the classes on which i am implementing join ,name as DepartmentProgram)

【问题讨论】:

  • 不要在您的问题中包含代码的图片 ....包含代码本身。

标签: c# .net ajax model-view-controller


【解决方案1】:

请您也发布您的模型吗?我假设,您正在使用延迟加载。我认为问题在于,实体包含导航属性,这些属性将在实体上下文已被释放后加载。

使用浏览器的开发工具查看服务器的响应。在那里你可以看到异常。

我敢打赌它会显示类似:

ObjectContext 实例已被释放,不能再使用 用于需要连接的操作。

【讨论】:

  • 这里是一个视图模型:using System;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;命名空间 SchoolCampus.Models { 公共类 DepartmentProgram { 公共 DEPARTMENT DEPARTMENT { 获取;放; } 公共程序程序 { 获取;放; } } }
  • 这是浏览器错误:POST localhost:55338/Default/Display 500 (Internal Server Error)
  • 请更新您的问题并添加类型“DepartmentProgram”、“DEPARTMENT”和“PROGRAM”。检查导致 500(内部服务器错误)的请求的“响应”。应该有更多的细节。
【解决方案2】:

2 个选项:

  1. 将您的结果映射到匿名对象列表:
  2. 创建一个新的 ViewModel:

    [Serializable]
    public class DepartmentProgramViewModel
    {
        public DepartmentViewModel Department { get; set; }
        public ProgramViewModel Program{ get; set; }
    }
    [Serializable]
    public class DepartmentViewModel
    {
        public string WhateverProperties{ get; set; }
    }
    [Serializable]
    public class ProgramViewModel 
    {
        public string WhateverProperties{ get; set; }
    }
    

    将您的查询结果映射到此视图模型。

我刚刚回答了另一个问题:500 error in Ajax Get

这个问题在新的 MVCer 中经常发生。不要尝试从上下文级数据结构返回数据列表,因为它们相互引用,不能自动序列化为json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多