0

By clicking submit button when form fields are not validated ReturnUrl from browser vanished.

Before clicking submit button browser seems look like.

enter image description here

After clicking submit button(while form fields are not validated till) browser seems look like.

enter image description here

How to maintain same browsing address after clicking submit button as after login it should be redirect to that page.?

Here is the Action code

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
            model.UserName,
            DateTime.Now,
            DateTime.Now.AddMinutes(30), // value of time out property
            false, // Value of IsPersistent property
            String.Empty,
            FormsAuthentication.FormsCookiePath);
            string encTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            Response.Cookies.Add(faCookie);
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            return RedirectToLocal(returnUrl ?? Url.Action("Index", "Home"));
        }
        else
        {
            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View();
        }
    }
    else
    {
        return View(model);
    }
}

Thanks in advance

2 Answers2

0

I would take a look at the AccountController at the public ActionResult Login(LoginModel model, string returnUrl) method.

You can check out this SO article: How am I supposed to use ReturnUrl = ViewBag.ReturnUrl in MVC 4

Community
  • 1
  • 1
jwatts1980
  • 7,254
  • 2
  • 28
  • 44
0

Assuming you view has

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { ...

Then when validation fails, you need to re-assign the value in you action method

if (ModelState.IsValid)
{
  ....
}
else
{
  // If we got this far, something failed, redisplay form
  ModelState.AddModelError("", "The user name or password provided is incorrect.");
  ViewBag.ReturnUrl = returnUrl; // Add this line
  return View();
}
  • Stephen... my view has this line. @using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "customForm",ReturnUrl = ViewBag.ReturnUrl })) – azhar_SE_nextbridge Aug 18 '14 at 05:49
  • Thats OK - my answer will still work, the key issue is the `..ReturnUrl = ViewBag.ReturnUrl..` Its value is assigned when you are redirected to this page for authentication, but you need to reassign it when you return the view because validation failed –  Aug 18 '14 at 05:54
  • i do the same as you suggest me but problem is that. returnUrl is coming null to action that't why it is not assigning to Viewbag.ReturnUrl. don't know why... – azhar_SE_nextbridge Aug 18 '14 at 05:57
  • If i write this line. @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) it works for me but it is neccassary to assign id to form i manage css with that id.. – azhar_SE_nextbridge Aug 18 '14 at 05:58
  • Sorry, just noticed you using the wrong overload. It should be `@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "customForm" })) –  Aug 18 '14 at 06:00