Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I want to redirect the user to another page when he is logged. I try this solution but the actual page is loading and not redirecting to other page.

The Address Bar conatin a new # at the end.

This is my code in Controller :

C#
[HttpPost]
    public ActionResult Login(FormCollection fc)
    {
        int res = dblayer.Admin_Login(fc["Email"], fc["Password"]);
        if (res == 1)
        {
            return View("~/Views/Client/Edit.cshtml");
        }
        else {
            return View("~/Views/Client/Create.cshtml");
        }
        return View();


    }


and this what I have wrote in View :

HTML
<div class="login" id="login">
        @*@RenderPage("~/Views/Home/Login.cshtml")*@
        <div class="main-w3l">
            <div class="w3layouts-main" style="background-image:url('/template/web/images/bg3.jpg'); margin-top:50px;">
                <h2>Login Now</h2>
                @using (Html.BeginForm("Login", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {

                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)

                }


                <form action="#" method="post">
                    <input value="E-MAIL" name="Email" type="email" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-Mail';}" />
                    <input value="PASSWORD" name="Password" type="password" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'password';}" />
                    <span><input type="checkbox" />Remember Me</span>
                    <h6><a href="#">Forgot Password?</a></h6>
                    <div class="clear"></div>
                    <input type="submit" value="login" name="login">
                </form>
                <p>Don't Have an Account ?<a href="#" onclick="@("window.location.href='" + @Url.Action("Create", "Client") + "'") ;">Register Now</a></p>
            </div>
        </div><pre lang="HTML">


and this is my db.cs :

C#
public int Admin_Login(string Email, string Password)
    {

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DeliveryCon"].ConnectionString)) { 
        int res = 0;
        SqlCommand com = new SqlCommand("SP_Login", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@Email", Email);
        com.Parameters.AddWithValue("@Password", Password);
        SqlParameter oblogin = new SqlParameter();
        oblogin.ParameterName = "@Isvalid";
        oblogin.SqlDbType = SqlDbType.Bit;
        oblogin.Direction = ParameterDirection.Output;
        com.Parameters.Add(oblogin);
        con.Open();
        com.ExecuteNonQuery();
        res = Convert.ToInt32(oblogin.Value);
        return res;
        }
    }


What I have tried:

the problem persist when I ve tried to test using Javascfript like this :

C#
HttpPost]
    public ActionResult Login(FormCollection fc)
    {
        int res = dblayer.Admin_Login(fc["Email"], fc["Password"]);
        if (res == 1)
        {
            TempData["msg"] = " Login Successful !";

        }
        else {
            TempData["msg"] = " Email or Password is wrong !";

        }
        return View();
  }


and in view this script :
JavaScript
@{
    if (TempData["msg"] != null)
    {
        <script type="text/javascript">
            alert('@TempData["msg"]');
        </script>
    }
}
Posted
Comments
F-ES Sitecore 13-Sep-17 5:35am    
You're creating two forms, the Html.BeginForm is a form and you then have an explicit form tag below it that has your fields in it. The action of that form is "#" so when the form is submitted that's where it goes.

You need to use BeginForm to create a form that submits to your login action.

You really need to get a better grasp of the basics of MVC, I'd suggest going through the music store tutorial to learn how forms and actions work. Google "mvc music store".
Anouar2002 13-Sep-17 6:59am    
ok thanks for the answer, yes you have right das was a mistake but when I fix it, the problem persist
Anouar2002 13-Sep-17 7:10am    
It works now ! thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900