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: