I'm trying to serialize the database model. But there is an error.
Here is my model:
public class Equipment
{
public int Id { get; set; }
//Двигатель
[Required]
[Display(Name = "Двигатель")]
public int Engine { get; set; }
//Количество лошадиных сил
[Required]
[Display(Name = "Мощность")]
public int Power { get; set; }
//Год выпуска
[Required]
[Display(Name = "Год выпуска")]
public int ReleaseYear { get; set; }
//Тип привода
[Required]
[Display(Name = "Тип привода")]
public string DriveType { get; set; }
//КПП
[Required]
[Display(Name = "КПП")]
public string Transmission { get; set; }
//Кузов
[Required]
[Display(Name = "Кузов")]
public string Body { get; set; }
//Максимальная скорость
[Required]
[Display(Name = "Максимальная скорость")]
public int MaxSpeed { get; set; }
//Вес
[Required]
[Display(Name = "Вес")]
public int Weight { get; set; }
//Бак
[Required]
[Display(Name = "Бак")]
public int MaxFuelVolume { get; set; }
//Цвет автомобиля
[Required]
[Display(Name = "Цвет")]
public string Color { get; set; }
//Изорбражение автомобиля
[Required]
[Display(Name = "Изображение автомобиля")]
public string Picture { get; set; }
public int CarModelId { get; set; }
public virtual CarModel CarModel { get; set; }
public virtual ICollection<Car> Cars { get; set; }
public Equipment()
{
Cars = new List<Car>();
}
}
Here is my code in the controller for serialization:
return Json(JsonConvert.SerializeObject(car.Equipment));
This is what error produces:
Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Equipment_4B2DAA6CED521984C9D15F5FEA2BB989D5DB7F80683721A0FC847726CB1C9ACB'. Path 'CarModel.Equipments'.
Here is the class that swears:
public class CarModel
{
public int Id { get; set; }
[Display(Name = "Модель")]
public string ModelName { get; set; }
public int MarkId { get; set; }
public virtual Mark Mark { get; set; }
public virtual ICollection<Equipment> Equipments { get; set; }
public CarModel()
{
Equipments = new List<Equipment>();
}
}
I tried to connect System.web.script.serialization
and set the script ignore attribute to
public virtual CarModel CarModel { get; set; }
public virtual ICollection<Car> Cars { get; set; }
In equipment class and on
public virtual ICollection<Equipment> Equipments { get; set; }
n the CarModel class, it's still the same error.
Please tell me how to avoid this error? And how can you serialize a model with cyclic dependency?
ajax request:
$('#autoList').on('change', function () {
var ddlAuto = $('#autoList').val();
if (!ddlAuto || ddlAuto === '')
return false;
$.ajax({
type: "POST",
url: "/Administration/GetCarInfo",
data: {
"carId": ddlAuto
},
success: function (data) {
alert('ОК');
}
});
});
But under one condition, it was possible to serialize, though it was serialized not into an object but into a string.
The result of serialization if in the controller to write
return Json(JsonConvert.SerializeObject(car.Equipment, Formatting.Indented, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
}));
"{
"$id": "1",
"CarModel": {
"$id": "2",
"Equipments": [
{
"$ref": "1"
}
],
"Mark": {
"$id": "3",
"CarModels": [
{
"$ref": "2"
},
{
"$id": "4",
"Equipments": [
{
"$id": "5",
"CarModel": {
"$ref": "4"
},
"Cars": [
{
"$id": "6",
"Equipment": {
"$ref": "5"
},
"Orders": [
{
"$id": "7",
"Cars": {
"$ref": "6"
},
"Clients": {
"$id": "8",
"Orders": [
{
"$ref": "7"
},
{
"$id": "9",
"Cars": {
"$id": "10",
"Equipment": {
"$id": "11",
"CarModel": {
"$ref": "4"
},
"Cars": [
{
"$ref": "10"
}
],
"Id": 30,
"Engine": 2500,
"Power": 170,
"ReleaseYear": 2011,
"DriveType": "Передний",
"Transmission": "Автомат",
"Body": "Седан",
"MaxSpeed": 220,
"Weight": 1800,
"MaxFuelVolume": 80,
"Color": "Чёрный"
I tried to do something else
return Json(JsonConvert.SerializeObject(car.Equipment, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}));
Then an error occurs:
"System.OutOfMemoryException"
Please tell me what am I doing wrong? How can I serialize my model into an object to continue working with it?