Reputation: 45
I am trying to return a list from MVC controller
. Value is returned easily when I am returning a hard code value. But when I am returning a list, it gives me an error. Here is my code ,
Ajax call,
function MyFunction() {
alert($('#DDlSurvey').val());
$.ajax({
url: "@Url.Action("Index", "ConductSurvey")",
data: { prefix: $('#DDlSurvey').val() },
type: "POST",
dataType: "json",
success: function (data) {
// loadData(data);
alert("Success");
// alert(data)
},
error: function (data){
alert("Failed! Please try again.");
}
});
//$('#YourLabelId').val('ReplaceWithThisValue');
}
and my method is,
[HttpPost]
public JsonResult Index(int prefix)
{
List<SelectList> Questions = new List<SelectList>();
List<Question> QuestionList = new List<Question>();
List<string> ll = new List<string>();
Question nn = new Question();
SurveyAppEntities ObjectSur = new SurveyAppEntities();
QuestionList = (from q in ObjectSur.Questions
join b in ObjectSur.SurveyQuestions on q.ID equals b.QuestionID
where b.SurveyID.Equals(prefix)
select q).ToList();
//return Json("OK");
return new JsonResult {Data=QuestionList, JsonRequestBehavior=JsonRequestBehavior.AllowGet};
}
When I return OK
i get it but when i try to return QuestionList It is not returning data and display Failed
I am also using one link for help in which every thing is as it is link is,
http://www.dotnetawesome.com/2014/05/how-to-retrieve-database-data-show-using-jquery-mvc-asp.html
Hopes for your suggestion
Upvotes: 0
Views: 487
Reputation: 24957
A circular reference was detected indicates that EF generates proxy objects when generating query results which cannot be serialized using JsonResult
. Assumed that SurveyAppEntities
is your DbContext
instance, then you need to do one of 2 options below:
1) Disable proxy creation
You need to set ProxyCreationEnabled
property to false
which prevent EF creating proxy objects which cannot be serialized:
[HttpPost]
public JsonResult Index(int prefix)
{
List<SelectList> Questions = new List<SelectList>();
List<Question> QuestionList = new List<Question>();
List<string> ll = new List<string>();
Question nn = new Question();
SurveyAppEntities ObjectSur = new SurveyAppEntities();
// this line is mandatory
ObjectSur.Configuration.ProxyCreationEnabled = false;
QuestionList = (from q in ObjectSur.Questions
join b in ObjectSur.SurveyQuestions on q.ID equals b.QuestionID
where b.SurveyID.Equals(prefix)
select q).ToList();
return Json(QuestionList, JsonRequestBehavior.AllowGet);
}
2) Disable query result tracking
EF implements query result tracking with proxy objects which may be disabled by adding AsNoTracking()
to each table entities:
[HttpPost]
public JsonResult Index(int prefix)
{
List<SelectList> Questions = new List<SelectList>();
List<Question> QuestionList = new List<Question>();
List<string> ll = new List<string>();
Question nn = new Question();
SurveyAppEntities ObjectSur = new SurveyAppEntities();
QuestionList = (from q in ObjectSur.Questions
join b in ObjectSur.SurveyQuestions on q.ID equals b.QuestionID
where b.SurveyID.Equals(prefix)
select q).AsNoTracking().ToList(); // prevents result tracking
return Json(QuestionList, JsonRequestBehavior.AllowGet);
}
Note:
You may also try to return only required properties instead of entire ObjectSur.Questions
object.
Related issue:
Circular reference detected exception while serializing object to JSON
Upvotes: 1
Reputation: 2212
Simply return list like below
return Json(QuestionList, JsonRequestBehavior.AllowGet);
Upvotes: 0