i have a login form in a partial view which resides in a modal in the main template (layout). Upon clicking on a button the login form appears and the user should fill that form, till yet everything is just fine but whenever the user clicks the login button the data is sent to the server for validation if the data was fine the user is redirected to website secure area but if not the user is lost because the page has been refreshed and the modal is not shown back so the user has to click back on login to preview the modal and then see the error! I would like to implement an ajax login where when the user clicks on login button if the data is correct should be redirected to website secure area else an error should be given. Here is the partial view for login.
@model Hire.af.WebUI.Models.LoginViewModel
<!--Login Form-->
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "noo-ajax-login-form form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
<div class="col-sm-9">
@Html.TextBoxFor(m => m.Email, new { @class = "log form-control", @placeholder="Email OR Username" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(m => m.Password, new { @class = "col-sm-3 control-label" })
<div class="col-sm-9">
@Html.PasswordFor(m => m.Password, new { @class = "pwd form-control", @placeholder="Password" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-sm-9 col-sm-offset-3">
<div class="checkbox">
<div class="form-control-flat">
<label class="checkbox">
@Html.CheckBoxFor(m => m.RememberMe, new { @class = "rememberme", @type = "checkbox" })
<i></i> Remember Me
</label>
</div>
</div>
</div>
</div>
<!--Remember Me Checkbox-->
<div class="form-actions form-group text-center">
<input type="submit" value="Sign In" class="btn btn-primary" />
<div class="login-form-links">
<span><a href="#"><i class="fa fa-question-circle"></i> Forgot Password?</a></span>
<span>
Don't have an account yet?
<a href="#" class="member-register-link" data-rel="registerModal">
@Html.ActionLink("Register Now", "Register") <i class="fa fa-long-arrow-right"></i>
</a>
</span>
</div>
</div>
@* Enable this once you have account confirmation enabled for password reset functionality
<p>
@Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>*@
}
any help would be appreciated. Thanks.
Edit1 for Samit Patel: Here is controller action method for Login
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
[Authorize]and if the user navigates to it, they will be redirected to your login page, and if successful, then to the page they were navigating to.return RedirectToAction()are pointless since ajax calls will never redirect.