0

I have a login page. This login page contains a register Partial view and a login partial view. Everything is showing up Ok.

Here is my controller for that page.

public ActionResult Login(string returnUrl)
{
    var model = new AccountPageViewModel();
    model.LoginModel = new LoginViewModel();
    model.RegisterModel = new RegisterViewModel();
    return View(model);
}

where I run into issues is posting from the partials and trying to return the model that I just posted, when there are errors in the model state.

Here is what my Login partial is posting to.

public async Task<ActionResult> _LoginPartial(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);
    }
}

Can someone explain how I would post from the Login partial, to a controller action and then return the data to the partialView rather than returning to the main Login view? I can't seem to find the answer.

Thanks.

KryptoBeard
  • 1,098
  • 9
  • 23
  • Are you posting using ajax? – JB06 Aug 12 '16 at 17:05
  • what issues are you running into ? that part is kind of important – vlscanner Aug 12 '16 at 17:06
  • No, just a normal post. The issue is when I post my login form from partial view, it hits my controller and tries to return the main login view, rather than just giving me back the response from the partial view. – KryptoBeard Aug 12 '16 at 17:07
  • 2
    You need to post using ajax if you don't want to do a full post-back. Example: http://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax – JB06 Aug 12 '16 at 17:11
  • Ok, so if I use an angular post hit my partial controller and do that, can I even use my MVC model for that page? How would I return that from the Ajax post in the controller. I get that I can return it with json to angular but I wanted to bind it to the page model – KryptoBeard Aug 12 '16 at 17:18

0 Answers0