I'm trying to convert an object containing a relationship and serializing affine, from sending it to View by Ajax
. I have two related classes
public class Eventos : IEntidade<EventosAuditoria>
{
[Key]
public int ID { get; set; }
public Guid ProfissionalId { get; set; }
[Required(ErrorMessage = "O Nome não pode ser em branco!")]
[StringLength(50, ErrorMessage = "Maximo de 30 caracteres!")]
public string title { get; set; }
public DateTime start { get; set; }
public String HoraEvento { get; set; }
//public String DuracaoEvento { get; set; }
public DateTime end { get; set; }
public bool Consulta { get; set; }
public bool Retorno { get; set; }
public String Observacoes { get; set; }
//relacionamento com tabela profissional
public virtual Profissional Profissional { get; set; }
}
And my Professional class
[Table("Profissional")]
public class Profissional : IEntidade<ProfissionalAuditoria>
{
[Key]
public Guid ProfissionalId { get; set; }
public String Nome { get; set; }
public bool Ativo { get; set; }
//Relacionamento com tabela eventos
public virtual ICollection<Eventos> Eventos { get; set; }
}
However when trying to enter the value of ProfessionalId on the screen, as follows:
public JsonResult ObtemPorId(int id)
{
var evento = Db.Eventos.FirstOrDefault(e => e.ID == id);
return Json(evento, JsonRequestBehavior.AllowGet);
}
It comes as 00000-0000-0000 ... So looking for some ways to solve it, I saw that with the Json.net
library it is possible. But, I had some implementation issues. Follow them below.
eventClick: function(calEvent, jsEvent, view) {
$.ajax({
url: '/Home/ObtemPorId',
Type: 'POST',
data: $('#ID').val(calEvent.ID),
success: function(response) {
$('#editEventTitle').val(response.title);
$('#editEventDate').val(response.start);
$('#editEventTime').val(response.end);
ModalAdicionar(response)//Aqui passo o os dados recebidos para o modal
}
});
function ModalAdicionar(date) {
ClearPopupFormValues();
$('#ModalAdicionar').modal('show');
$('#eventTitle').focus();
}
In my controller, I try to return serialized data
public JsonResult ObtemPorId(int id)
{
var evento = Db.Eventos.FirstOrDefault(e => e.ID == id);
var result = JsonConvert.SerializeObject(evento, Formatting.Indented,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return Json(result, JsonRequestBehavior.AllowGet);
}
However, the result that comes to me in Response is something like this
"Profissional": {
"Eventos": [
{
"ID": 3,
"ProfissionalId": "47961ca9-4a6e-451b-b984-2123134587fa",
"title": "Renan Carlos",
"start": "2017-05-31T12:00:00",
"HoraEvento": null,
"end": "2017-05-31T12:30:00",
"Consulta": false,
"Retorno": false,
"Observacoes": null,
"DataCriacao": "2017-05-30T15:14:37.053",
"UsuarioCriacao": "[email protected]",
"UltimaModificacao": null,
"UsuarioModificacao": null
},
{
"ID": 4,
"ProfissionalId": "47961ca9-4a6e-451b-b984-2123134587fa",
"title": "Renan",
"start": "2017-05-31T11:30:00",
"HoraEvento": null,
"end": "2017-05-31T12:00:00",
"Consulta": false,
"Retorno": false,
"Observacoes": null,
"DataCriacao": "2017-05-30T15:32:08.897",
"UsuarioCriacao": "[email protected]",
"UltimaModificacao": null,
"UsuarioModificacao": null ...
That is something, quite different from what was expected. And changing this line
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
To:
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize});
I have this message:
'System.Data.Entity.DynamicProxies.Events_E1047ACBD21CEA4B87C1021CE8FEFEDFE86C5AC747E6D7FF7170ADD1A1872419'.
How can I do this implementation? Ps: I need to pass the information of ProfessionalId, contained in Events.