Open modal through Actionresult in MVC

Asked

Viewed 3,205 times

0

Hello, I’m building a registration page with MVC . NET and I’m having a hard time. I want to validate the filled information, if they are correct, I will follow the application flow but if not, I want to open a jquery modal alerting the user that there is incorrect information. Since I’m new to MVC, I haven’t got that yet. I created the div with modal and call through the button click, but I could not put this condition to open only when my post is not successful.

Below follows the div with the modal I created.

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">
                        <h4>ATENÇÃO!</h4>
                        <p>
                            Existem informações inconsistentes em seu cadastro.
                        </p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Fechar</button>
                    </div>
                </div>
            </div>
        </div>

1 answer

1


So come on...

A simple example...

Contoller:

public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(String user, String senha)
{
    if (user == "user" && senha == "senha")
        //Acesso autorizado
        return RedirectToAction("PaginaUser");
    else
    {
        ViewBag.Message = "Acesso Negado";
        return View();
    }

}

public ActionResult PaginaUser()
{
    return View();
}

View:

<h2>Login</h2>
@if (ViewBag.Message == "Acesso Negado")
{
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <h4>ACESSO NEGADO!</h4>
                <p>
                    Verifique seu login e senha.
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Fechar</button>
            </div>
        </div>
    </div>
</div>
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Login</h4>
    <hr />
    <div class="form-group">
        @Html.Label("Login", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.User, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Senha", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Login" class="btn btn-default" />
        </div>
    </div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
    $(document).ready(function () {
        $('#myModal').modal('show');
    });
</script>
}

Browser other questions tagged

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