Error 500 in ajax request with asp.net mvc

2

Good morning,

I have a problem that I can not resolve. I made a simple ajax request in my code to fill in the fields automatically if the cpf entered is already registered in the database.

Well, on the day I did everything worked and was working a thousand wonders, but now out of nowhere, he is giving Error 500 and says he does not find my Action being that he goes into action, goes to the bank, gets the result , but when it returns it does not go to View.

Can someone please help me?!

public class ParticipanteController : Controller
{
    mconfEntities db = new mconfEntities();
    // GET: Participante
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public JsonResult BuscaDados(string cpf)
    {
        string pesquisaCpf = cpf.Replace(".", "");

        var dados = db.Participante.FirstOrDefault(p => p.cpf == pesquisaCpf);

        return Json(dados);
    }

    public ActionResult Create()
    {
        var evento = db.Evento;

        ViewBag.conferencia = new SelectList(evento, "eventoUID", "descricao");

        return View();
    }

    [HttpPost]
    public ActionResult Create(Participante model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                Participante p = new Participante()
                {
                    cpf = model.cpf,
                    nome = model.nome,
                    email = model.email,
                    telefone = model.telefone,
                    municipio = model.municipio,
                    unidadeSaude = model.unidadeSaude,
                    equipeSaude = model.equipeSaude,
                    categoriaProfissional = model.categoriaProfissional
                };

                db.Participante.Add(p);
                db.SaveChanges();

                return Redirect("http://www.google.com");
            }
            catch (EntityException ex)
            {
                TempData["erro"] = "Erro ao cadastrar participante - " + ex.Message;
                return View();
            }
        }
        else
        {
            TempData["erro"] = "Participante Inválido";
            return RedirectToAction("Create");
        }
    }
}


<script>
$(document).ready(

function () {       $ ('# cpf'). focusout (function {) {           debugger;           var

      $.ajax
      ({
          url: '/Participante/BuscaDados/',
          type: "POST",
          data: { cpf: cpfData },
          success: function (data) {
              $('#nome').val(data.nome);
              $('#email').val(data.email);
              $('#telefone').val(data.telefone);
              $('#municipio').val(data.município);
              $('#unidadeSaude').val(data.unidadeSaude);
              $('#equipeSaude').val(data.equipeSaude);
              $('#categoriaProfissional').val(data.categoriaProfissional);
          }
      });
  });

});

The error:

    
asked by anonymous 03.07.2015 / 15:13

2 answers

1

I was able to solve, after a few days without sleeping and using the Fiddler that Fernando Medeiros indicated, I discovered that it was a Circular reference problem in the classes created by the entity framework.

I solved by putting a .Select(t => new {t.nome, t.email, ...}) before the .Where () and this way it could identify which table I was wanting to get the data.

The strangest thing in my case is that the day I made the Code the stop worked and suddenly stopped working ...

Thank you to all who helped ...

Solution:

[HttpGet]
    public JsonResult BuscaDados(string cpf)
    {      
        var dados = db.Participante.Select(t => new {t.cpf, t.nome, t.email, t.telefone, t.municipio, t.unidadeSaude, t.equipeSaude, t.categoriaProfissional } ).Where(p => p.cpf == cpf);

        return Json(dados, JsonRequestBehavior.AllowGet);
    }
    
07.07.2015 / 12:27
3

I went through something similar these days,

Use the fiddler program to check which requests and which responses you are sending.

Regarding the error can be connected to the return type, try the return as follows:

return Json(dados, JsonRequestBehavior.AllowGet);

or

//retorna uma string json
return JavaScriptSerializer.Serialize(dados);

Now use the Fiddler to check if the response is being sent containing JSON

    
03.07.2015 / 16:13