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 :
[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 :
<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 :
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 :
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 :
@{
if (TempData["msg"] != null)
{
<script type="text/javascript">
alert('@TempData["msg"]');
</script>
}
}