Asp.net MVC form, no modal call after data validation

0

I tried to make the modal call through the ViewBag, but without success. I made a Debugg to see how the process was and in the final step that would appear the modal informing the user that "contains duplicate data", it does not execute.

// Controller

[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 retorno = new ProspectBLL().VerificaProspectExiste(model.Prospect);

  //Já tem usuário cadastrado
  if (retorno == false) {
    var prospAdd = new ProspectBLL().AdicionarProspect(model.Prospect);

    if (prospAdd == 0) {
      return RedirectToAction("Index", "Prospect", new {
        mensagem = "Erro no Cadastro."
      });
    } else {
      return RedirectToAction("Index", "Prospect");
    }
  } else {
    //return RedirectToAction("Index", "Prospect", new { messagem = "Erro no Cadastro." }); 
    ViewBag.Message = "Acesso Negado";
    return RedirectToAction("Index", "Prospect");
  }

}
//Na View:

<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">&times;</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>

<!------------------------Modal--------------------------->
<div class="container">
  <!-- Modal -->
  <div class="modal" id="myModalAlert" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Atenção !</h4>
        </div>
        <div class="modal-body">
          <p>Contém dados já cadastrados !</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>
</div>

//
@if (ViewBag.Message == "Acesso Negado")
{
    //Já chamei a função aqui, mas não executou. Talvez não tenha chamado da maneira correta.
}

<script>
    function chamaModal() {
        $("#myModalAlert").show();
    }
</script>
    
asked by anonymous 02.08.2018 / 16:31

1 answer

0

Your @if must be within the JavaScript block

<script>
    function chamaModal() {
        $("#myModalAlert").show();
    }

   @if (ViewBag.Message == "Acesso Negado")
   {
     chamaModal();
   }
</script>

In addition, the RedirectToAction(); method does not transport the ViewModel to the destination, only the route attributes, the correct thing is to use return View() .

    
02.08.2018 / 17:11