1
I’m having a hard time with the duplicity record. How do I so that after the user fills in the form, at the time of saving, the data is compared to what is in the database, if he has any repeated field he does not save the form data?
[HttpPost]
public ActionResult CadastrarProspect(string prospectnome, string prospectemail, string prospectcelular, string prospecttelefone, string prospectanotacao)
{
//Pegar Vendedor Logado
var model = new ProspectViewModel();
model.Prospect.IdVendedor = VariaveisDeSessao.VendedorLogado.IdVendedor;
model.Prospect.DsNome = prospectnome;
model.Prospect.DsEmail = prospectemail;
model.Prospect.DsCelular = prospectcelular.Replace("-", "").Replace(".", "").Replace("(", "").Replace(")", "").Replace(" ", "");
model.Prospect.DsTelefone = prospecttelefone.Replace("-", "").Replace(".", "").Replace("(", "").Replace(")", "").Replace(" ", "");
model.Prospect.DsAnotacao = prospectanotacao;
var prospAdd = new ProspectBLL().AdicionarProspect(model.Prospect);
if (prospAdd == 0)
{
return RedirectToAction("Index", "Prospect", new { mensagem = "Erro no Cadastro." });
}
else
{
return RedirectToAction("Index", "Prospect");
}
}
public class ProspectBLL : BaseBLL
{
//Adicionar Prospect
public long AdicionarProspect(Prospect prospect)
{
try
{
prospect.FgAtivo = true;
prospect.FgExcluido = false;
prospect.DtCriacao = DateTime.Now;
prospect.DtAlteracao = DateTime.Now;
banco.Prospect.Add(prospect);
return banco.SaveChanges();
}
catch (Exception ex)
{
new Logger.Logger().FazerLogAsync(ex, new { Tipo = "Erro", Mensagem = "Erro ao adicionar novo vendedor" }, Logger.EnumTiposDeLog.TiposDeLog.Erro);
return 0;
}
}
<div class="modal" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="close">×</button>
<h3 class="modal-title">Novo prospect</h3>
</div>
<form method="post" action="/Prospect/CadastrarProspect">
<div class="modal-body">
<div class="form-group">
<label for="nome" class="col-form-label">Nome*</label>
<input type="text" class="form-control" id="nomeprospect" name="prospectnome" required>
</div>
<div class="form-group">
<label for="email" class="col-form-label">E-mail</label>
<input type="text" class="form-control" id="emailprospect" name="prospectemail">
</div>
<div class="form-group">
<label for="celular" class="col-form-label">Celular*</label>
<input type="text" class="form-control" id="celularprospect" name="prospectcelular" required>
</div>
<div class="form-group">
<label for="telefone" class="col-form-label">Telefone</label>
<input type="text" class="form-control" id="telefoneprospect" name="prospecttelefone">
</div>
<div class="form-group">
<label for="anotacao" class="col-form-label">Anotação</label>
<textarea class="form-control" id="anotacaoprospect" name="prospectanotacao" rows="4" cols="50"></textarea>
</div>
<div class="form-group">
<label id="camposobrigatorios" class="col-form-label">*Campos obrigatórios</label>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="btnOK">Salvar</button>
</div>
</form>
</div>
</div>
</div>
Could you edit your question and add your current code? We need to see how your application model is doing to help you.
– Pedro Paulo