How to call a modal by ActionResult of the Controller, after the user clicks the button to register?
Currently the controller is redirecting to the index page after the user signs up. I want instead of redirect, call the modal that is in the view, because in the modal I will put a directional button for the index.
Contoller:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Create([Bind(Include = "Id,Nome,Email,Senha")] Pessoas pessoas)
{
if (ModelState.IsValid)
{
db.Pessoas.Add(pessoas);
db.SaveChanges();
return Json("s");
}
return Json("n");
}
Ajax:
<script>
$(".cadastroajax").click(function () {
var id = $(this).attr("rel");
$.ajax({
dataType: "json",
type: "POST",
url: "/Pessoas/Teste",
data: { id },
success: function () {
location.reload();
}
});
});
</script>
View:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group col-sm-12">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
</div>
<div class="form-group col-sm-6">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
<div class="form-group col-sm-6">
@Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control", @maxlength = "32" } })
@Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
</div>
<div class="form-group col-sm-12">
<div class="botao-salvar">
<a class="btn btn-danger" href="@Url.Action("Index", "Pessoas")" role="button">
Cancelar
</a>
<button type="submit" value="Create" class="btn btn-success cadastroajax">
Cadastrar
</button>
</div>
</div>
}
<!-- Modal -->
<div class="modal fade" id="modalSucesso" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Sucesso!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Cadastro efetuado com sucesso.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>