2
I’m trying to convert an object that contains a relationship and Serializando afim, to send 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; }
}
But when trying to inform 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 resolve, I saw that with the library Json.net is possible. But, I had some problems in the implementation. 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();
        }
and in my controller, I try to return the data serialized
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 the Response is something like that
"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 ...
Which is something quite different than expected. And changing this line
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
To:
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize});
I have this message:
'System.Data.Entity.DynamicProxies.Eventos_e1047acbd21cea4b87c1021ce8fefedfe86c5ac747e6d7ff7170add1a1872419'.
How can I do this implementation ? Ps: I need to pass the information of Professionalid, contained in Events.
If you only need Professionalid, could just use
return Content(evento.ProfissionalId.ToString());– Ricardo Pontual
@Ricardopunctual I expressed myself badly, I did the editing
– Renan Carlos