views:

311

answers:

1

I have parent view that also renders sub-controller action using RenderAction() (that returns a PartialView). An example is a front page with Login partial view (inputs: username, password, remember and action: login)

Execution process

  1. GET for Home/Index - also displays my login control that has its login pointing to sub controller User/Login
  2. User enters credentials and clicks login
  3. POST for User/Login - checks credentials and returns ???

Problem

How do I return back to parent view from my sub controller action User/Login?
My sub controller's partial view can be rendered any page, so I can't just easily return result of parent controller action like:

return new HomeController().Index();

So how should I process my sub controller action and its partial view?

EDIT

I could post back to my sub-controller action with additional data of the parent route, but I also populate data in my sub-controller action. In my example I have to display that someone's credential weren't valid. A redirect would lose these...

+1  A: 

Instead of posting to /User/Login for performing the login, append a return url, so you would end up with /User/Login?returnUrl=/Home/Index (url encoding might change that a bit)

If returnUrl is set in the querystring, the action for /User/Login should just return a redirect action back to that returnUrl.

Sorry if that was a little unclear

LorenVS
It is clear. But let me add some *EDIT* to my question. the thing is if I populate ie. ModelState.Errors they will get lost on redirect, won't they?
Robert Koritnik
Most sites do this by having a dedicated login page that displays errors if the credentials are invalid. If the authentication is successfull, the user is redirected to the returnUrl (or the defaultUrl if returnUrl doesn't exist). If the authentication fails, the user stays on /User/Login. /User/Login contains a login form and an area to display the errors...
LorenVS