2
Good morning,
I got a problem I can’t fix. I made a simple ajax request in my code to fill the fields automatically if Cpf is already registered in the database.
Well, the day I did everything worked out and it was working great, but now out of the blue, he’s giving Error 500 and says q can’t find my Action and he enters the action, goes to the bank, gets the result, but when he returns he won’t 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 cpfData = $('#Cpf'). val(). replace('.','). replace('-','');
$.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 mistake:
Why post pictures of the code? Post the code. I’ll give you a hint on how to set a good example, follow the link: http://answall.com/help/mcve
– Guilherme Nascimento
Thanks for the tip, I’m new here. A question: I should delete this post and redo it or change it?
– Diego Lobo
Do not need to delete, just edit. Note that below the question has 4 links: share, edit, close and flag. Use "Edit".
– Guilherme Nascimento
All right, I’m done with the amendment
– Diego Lobo