How to call a modal in View by Controller's ActionResult

0

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">&times;</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>
    
asked by anonymous 25.10.2018 / 17:48

1 answer

1

You can do this request via ajax and instead of returning the view in actionresult, return only one json indicating whether or not something like { success: true } was successful. and in ajax return show modal via javascript using bootstrap.js $('#modalSucesso').modal('show');

Another option would be to return the html of your modal as a partial view and add it with .load () in the DOM. in this case you would have to take the CSS class fade of modal

    
25.10.2018 / 18:13