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

Asked

Viewed 149 times

0

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

//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>

1 answer

0

Your @if needs to be inside the Javascript block

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

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

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

  • Then Leandro, I located the error... The Redirecttoaction Return("Index", "Prospect"); after the View was "killing"the View. I switched to Return View("Index") and it worked !

  • I added the remark too

  • Ah, had not seen ! rs So, it works but it is not returning the content, that would be the list of registered have some idea ?

  • You can add this information to your Viewmodel and use in your Modal

  • I understood, I went to do a test with the code that you posted, but gives as: "The name named Modal does not exist in the current context". But if I keep it the way it is, just change Redirecttoaction(); to View(). Then it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.