Я хотел бы сериализовать объект List<Question>
в данные JSON.
Код, который я сейчас использую в приложении ASP.NET MVC:
List<Question> questionList;
questionList = questionManager.GetquestionsByTestId(id);
var listData = questionList.ToArray();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string strData = serializer.Serialize(listData);
JsonResult json = Json(strData, JsonRequestBehavior.AllowGet);
Код, похоже, приводит к следующей ошибке:
A circular reference was detected while serializing an object of type 'TestEnvironment.Models.Question'.
что должна означать эта ошибка?
Отредактировано: Вопрос класса
public partial class Question : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _QuestionId;
private string _QuestionText;
private System.Nullable<int> _TopicId;
private System.Nullable<int> _Marks;
private System.Nullable<int> _QuestionTypeId;
private System.Nullable<int> _ChapterId;
private System.Nullable<int> _Weightage;
private System.Nullable<System.TimeSpan> _ExpectedTimeToAnswer;
private EntitySet<Answer> _Answers;
private EntitySet<TestDetail> _TestDetails;
private EntityRef<Chapter> _Chapter;
private EntityRef<QuestionType> _QuestionType;
private EntityRef<Topic> _Topic;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnQuestionIdChanging(int value);
partial void OnQuestionIdChanged();
partial void OnQuestionTextChanging(string value);
partial void OnQuestionTextChanged();
partial void OnTopicIdChanging(System.Nullable<int> value);
partial void OnTopicIdChanged();
partial void OnMarksChanging(System.Nullable<int> value);
partial void OnMarksChanged();
partial void OnQuestionTypeIdChanging(System.Nullable<int> value);
partial void OnQuestionTypeIdChanged();
partial void OnChapterIdChanging(System.Nullable<int> value);
partial void OnChapterIdChanged();
partial void OnWeightageChanging(System.Nullable<int> value);
partial void OnWeightageChanged();
partial void OnExpectedTimeToAnswerChanging(System.Nullable<System.TimeSpan> value);
partial void OnExpectedTimeToAnswerChanged();
#endregion
public Question()
{
this._Answers = new EntitySet<Answer>(new Action<Answer>(this.attach_Answers), new Action<Answer>(this.detach_Answers));
this._TestDetails = new EntitySet<TestDetail>(new Action<TestDetail>(this.attach_TestDetails), new Action<TestDetail>(this.detach_TestDetails));
this._Chapter = default(EntityRef<Chapter>);
this._QuestionType = default(EntityRef<QuestionType>);
this._Topic = default(EntityRef<Topic>);
OnCreated();
}
[Column(Storage="_QuestionId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int QuestionId
{
get
{
return this._QuestionId;
}
set
{
if ((this._QuestionId != value))
{
this.OnQuestionIdChanging(value);
this.SendPropertyChanging();
this._QuestionId = value;
this.SendPropertyChanged("QuestionId");
this.OnQuestionIdChanged();
}
}
}
[Column(Storage="_QuestionText", DbType="NVarChar(MAX)")]
public string QuestionText
{
get
{
return this._QuestionText;
}
set
{
if ((this._QuestionText != value))
{
this.OnQuestionTextChanging(value);
this.SendPropertyChanging();
this._QuestionText = value;
this.SendPropertyChanged("QuestionText");
this.OnQuestionTextChanged();
}
}
}
[Column(Storage="_TopicId", DbType="Int")]
public System.Nullable<int> TopicId
{
get
{
return this._TopicId;
}
set
{
if ((this._TopicId != value))
{
if (this._Topic.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnTopicIdChanging(value);
this.SendPropertyChanging();
this._TopicId = value;
this.SendPropertyChanged("TopicId");
this.OnTopicIdChanged();
}
}
}
[Column(Storage="_Marks", DbType="Int")]
public System.Nullable<int> Marks
{
get
{
return this._Marks;
}
set
{
if ((this._Marks != value))
{
this.OnMarksChanging(value);
this.SendPropertyChanging();
this._Marks = value;
this.SendPropertyChanged("Marks");
this.OnMarksChanged();
}
}
}
[Column(Storage="_QuestionTypeId", DbType="Int")]
public System.Nullable<int> QuestionTypeId
{
get
{
return this._QuestionTypeId;
}
set
{
if ((this._QuestionTypeId != value))
{
if (this._QuestionType.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnQuestionTypeIdChanging(value);
this.SendPropertyChanging();
this._QuestionTypeId = value;
this.SendPropertyChanged("QuestionTypeId");
this.OnQuestionTypeIdChanged();
}
}
}
[Column(Storage="_ChapterId", DbType="Int")]
public System.Nullable<int> ChapterId
{
get
{
return this._ChapterId;
}
set
{
if ((this._ChapterId != value))
{
if (this._Chapter.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnChapterIdChanging(value);
this.SendPropertyChanging();
this._ChapterId = value;
this.SendPropertyChanged("ChapterId");
this.OnChapterIdChanged();
}
}
}
[Column(Storage="_Weightage", DbType="Int")]
public System.Nullable<int> Weightage
{
get
{
return this._Weightage;
}
set
{
if ((this._Weightage != value))
{
this.OnWeightageChanging(value);
this.SendPropertyChanging();
this._Weightage = value;
this.SendPropertyChanged("Weightage");
this.OnWeightageChanged();
}
}
}
[Column(Storage="_ExpectedTimeToAnswer", DbType="Time")]
public System.Nullable<System.TimeSpan> ExpectedTimeToAnswer
{
get
{
return this._ExpectedTimeToAnswer;
}
set
{
if ((this._ExpectedTimeToAnswer != value))
{
this.OnExpectedTimeToAnswerChanging(value);
this.SendPropertyChanging();
this._ExpectedTimeToAnswer = value;
this.SendPropertyChanged("ExpectedTimeToAnswer");
this.OnExpectedTimeToAnswerChanged();
}
}
}
[Association(Name="Question_Answer", Storage="_Answers", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<Answer> Answers
{
get
{
return this._Answers;
}
set
{
this._Answers.Assign(value);
}
}
[Association(Name="Question_TestDetail", Storage="_TestDetails", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<TestDetail> TestDetails
{
get
{
return this._TestDetails;
}
set
{
this._TestDetails.Assign(value);
}
}
[Association(Name="Chapter_Question", Storage="_Chapter", ThisKey="ChapterId", OtherKey="ChapterId", IsForeignKey=true)]
public Chapter Chapter
{
get
{
return this._Chapter.Entity;
}
set
{
Chapter previousValue = this._Chapter.Entity;
if (((previousValue != value)
|| (this._Chapter.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Chapter.Entity = null;
previousValue.Questions.Remove(this);
}
this._Chapter.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._ChapterId = value.ChapterId;
}
else
{
this._ChapterId = default(Nullable<int>);
}
this.SendPropertyChanged("Chapter");
}
}
}
[Association(Name="QuestionType_Question", Storage="_QuestionType", ThisKey="QuestionTypeId", OtherKey="QuestionTypeId", IsForeignKey=true)]
public QuestionType QuestionType
{
get
{
return this._QuestionType.Entity;
}
set
{
QuestionType previousValue = this._QuestionType.Entity;
if (((previousValue != value)
|| (this._QuestionType.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._QuestionType.Entity = null;
previousValue.Questions.Remove(this);
}
this._QuestionType.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._QuestionTypeId = value.QuestionTypeId;
}
else
{
this._QuestionTypeId = default(Nullable<int>);
}
this.SendPropertyChanged("QuestionType");
}
}
}
[Association(Name="Topic_Question", Storage="_Topic", ThisKey="TopicId", OtherKey="TopicId", IsForeignKey=true)]
public Topic Topic
{
get
{
return this._Topic.Entity;
}
set
{
Topic previousValue = this._Topic.Entity;
if (((previousValue != value)
|| (this._Topic.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Topic.Entity = null;
previousValue.Questions.Remove(this);
}
this._Topic.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._TopicId = value.TopicId;
}
else
{
this._TopicId = default(Nullable<int>);
}
this.SendPropertyChanged("Topic");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
private void attach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
}