Login to index screen

Asked

Viewed 269 times

0

Talk to the guys, all right? So I’m developing an application in ASP.NET MVC and I have a login screen, but I include in the login navbar, where the index the user could enter his email and password and click the login button, without needing to be directed to another page, but this does not happen, when he clicks login does not work, just give a refresh on the page. I believe he would have to run the controller, but he won’t... what can I do?

This is my login page:

@model IEnumerable<Tribus.Models.Pessoa>
@{
    Layout = "~/Views/Shared/_LayoutUser.cshtml";
    ViewBag.Title = "Login";
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h1>
            <p class="text-center"><b>Entrar no aplicativo</b></p>
        </h1>
        <hr />
        <h3 class="text-center">@ViewBag.Error</h3>
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <h1><b>Bem vindo a Tribus</b></h1>
        <div class="form-group">
            <label class="control-label col-md-2">
                Email
            </label>
            <div>
                <div class="col-md-10 ">
                    <input type="text" name="email" id="email" class="form-control" />
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">
                Senha
            </label>
            <div class="col-md-10">
                <input type="password" name="senha" id="senha" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Entrar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

this is my _layout page

<!DOCTYPE html>
<html>
@*NÃO MEXER EDITADO POR GABRIEL*@
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @{
        Tribus.Models.Pessoa pessoa = Tribus.Repositorios.Funcoes.GetUsuario();
        string nome;
        int acesso = 0;
        int id = 0;


        if (pessoa == null)
        {

            <div class="navbar navbar-default navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        @Html.ActionLink("Tribus", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })

                        @Html.ActionLink("Login", "Logar", "Publico", new { area = "" }, new { @class = "navbar-brand" })
                    </div>
                    <center>
                        <div class="navbar-collapse collapse" id="navbar-main">

                            <form class="navbar-form navbar-right" role="search">
                                @*@using (Html.BeginForm())
                                    {
                                        @Html.AntiForgeryToken()*@
                                <div class="form-group">
                                    <input type="text" id="email" class="form-control" name="email" placeholder="E-mail" required="required" autocomplete="on" />
                                </div>
                                <div class="form-group">
                                    <input type="password" id="senha" class="form-control" name="senha" placeholder="Senha" required="required" autocomplete="off" />
                                </div>
                                <input type="submit" value="Entrar" class="btn btn-default" />
                                @*}*@
                            </form>
                        </div>
                    </center>
                </div>
            </div>
        }
        else if (pessoa != null)
        {
            id = pessoa.PessoaID;
            nome = pessoa.Nome;
            <div class="navbar navbar-default navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        @Html.ActionLink("Tribus", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                        <ul class="nav navbar-nav">
                            <li>@Html.ActionLink("Sair", "Logoff", "Publico")</li>
                            @*<li>@nome</li>*@
                            Bem vindo @nome
                        </ul>
                    </div>
                </div>
            </div>
        }
        else
        {

        }
    }
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p class="text-center">&copy; @DateTime.Now.Year - Tribus- Oruam</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/inputmask")
    @RenderSection("scripts", required: false)
</body>
</html>

and this is my home page

@{
    ViewBag.Title = "Home Page";
}
<div class="">
    <h1>Tribus</h1>
    <p class="lead text-justify">Apresentação do aplicativo</p>

    <p class="lead text-center">Não é cadastrado? Cadastra-se!</p>
    <div class="row">
        <div class="col-md-12 text-center">
            <p><a href="~/Musico/Create" class="btn btn-primary btn-lg">Sou Músico </a></p>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 text-center">
            <p><a href="~/Usuario/Create" class="btn btn-primary btn-lg">Sou Usuário</a></p>
        </div>
    </div>

</div>

this is the Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tribus.Models;
using Tribus.Repositorios;

namespace Tribus.Controllers
{
    public class PublicoController : Controller
    {
        // GET: Publico
        public ActionResult Logar()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Logar(string email, string senha)
        {
            if (Funcoes.AutenticarUsuario(email, senha) == false)
            {
                ViewBag.Error = "Nome de usuário e/ou senha inválida";
                return View();
            }
            return RedirectToAction("Index", "Home");
        }
        public ActionResult AcessoNegado()
        {
            using (Context c = new Context())
            {
                return View();
            }
        }
        public ActionResult Logoff()
        {
            Tribus.Repositorios.Funcoes.Deslogar();
            return RedirectToAction("Index", "Home");
        }
    }
}

1 answer

3


This is very wrong (don’t get me wrong):

<form class="navbar-form navbar-right" role="search">
    @*@using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()*@
    <div class="form-group">
        <input type="text" id="email" class="form-control" name="email" placeholder="E-mail" required="required" autocomplete="on" />
    </div>
    <div class="form-group">
        <input type="password" id="senha" class="form-control" name="senha" placeholder="Senha" required="required" autocomplete="off" />
    </div>
    <input type="submit" value="Entrar" class="btn btn-default" />
    @*}*@
</form>

Your form is performing nothing, so to speak.

Remove the form in HTML and change to Razor, as in your login screen, this way:

@using (Html.BeginForm("Logar", "Publico", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h1>
            <p class="text-center"><b>Entrar no aplicativo</b></p>
        </h1>
        <hr />
        <h3 class="text-center">@ViewBag.Error</h3>
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <h1><b>Bem vindo a Tribus</b></h1>
        <div class="form-group">
            <label class="control-label col-md-2">
                Email
            </label>
            <div>
                <div class="col-md-10 ">
                    <input type="text" name="email" id="email" class="form-control" />
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">
                Senha
            </label>
            <div class="col-md-10">
                <input type="password" name="senha" id="senha" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Entrar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

In short, the lack of this part which is your problem:

   @using (Html.BeginForm("Logar", "Publico", FormMethod.Post))

With that, your form will be as follows:

<form action="/Publico/Logar" method="post">
  • Thank you Randrade! solved my problem!!

Browser other questions tagged

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