-1

hye everyone! i am using ajax to send and retrieve data ,, i am actually sending data to MVC controller against which i am get a data from two tables on the basis of join in var type variable, but the problem is when i send the result from the query to ajax , it fires error function instead of success.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)

rory.ap
  • 34,009
  • 10
  • 83
  • 174

2 Answers2

0

Please could you also post your model? I assume, you are using lazy loading. I think the problem is, that the entities contain navigation properties, which would be loaded after the entity context has already been disposed.

Use your browser's developer tools to look at the response form the server. There you can see the exception.

I bet it would show something like:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

marcelion
  • 76
  • 3
  • here is a view model: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SchoolCampus.Models { public class DepartmentProgram { public DEPARTMENT DEPARTMENT { get; set; } public PROGRAM PROGRAM { get; set; } } } – Haider Mustafa Sep 22 '16 at 03:36
  • and here is the browser error: POST http://localhost:55338/Default/Display 500 (Internal Server Error) – Haider Mustafa Sep 22 '16 at 03:38
  • Please update your question and add the types "DepartmentProgram", "DEPARTMENT" and "PROGRAM". Check the "response" of the request resulting in 500 (Internal Server Error). There should be additional details. – marcelion Sep 22 '16 at 09:06
0

2 options:

  1. map your result to anonymous object list:
  2. create a new 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; }
    }
    

    Map your query result to this view model.

I just answered another question: 500 error in Ajax Get

the issue happens a lot in new MVCer. Don't try to return data list from context level data structure, because they reference each other, cannot be serialized to json automatically.

Dongdong
  • 2,208
  • 19
  • 28