Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Controller, RedirectToAction and HttpPost

Vote:
 

Hi, I'm new to EPiServer MVC but have worked on some simple MVC projects before. I'm stumbeling to get some of the usuall MVC-functionallity to work and have 2 questions :).

 

 

Question #1:

One of these project had an LogOn controller (get) and a View that when submitting sent me to my LogOn controller (post). I can't get this to work in EPiServer, I'm sent to my LogOn(post) when submitting but the model is always null..

What am I missing?.

 

 

[HttpPost]

public ActionResult LogOn(LogOnModel model, string returnUrl)

{

// Empty model..

}

 

 

And question #2:

Is it possible to RedirectToAction in EPiServer mvc?. I can only get this to work if I redirect to a controller action in the same controller as the Redirect-statement... (I want to from ControllerA redirect to action in ControllerB).

RedirectToAction("Index", "SomeOtherController");

#70731
Apr 29, 2013 10:37
Vote:
 

For the first question - can you post view code as well?

For second question - in EPiServer page controllers usually are used to to serve incoming requests for page instances. So for example if you have page Page1 and it has coresponding page controller and editor created 2 pages with this page type:

 

Root -> Start Page -> Page1 type instance 1st

Root -> Start Page -> Page1 type instance 2nd

 

Question is - to which page instanc you want to redirect? I would assume that maybe you want to store PageReference property in some particular page type and use that as content reference to get page's url and then make a redirect.

For instance: after submitting form you want to show "Thank You page" (code could be something like this - in controller method):

 

var resultPageReference = currentPage.ThankYouPage;
var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var resultPage = contentLoader.Get<SitePageData>(resultPageReference);
var pageUrl = urlResolver.GetVirtualPath(resultPage.ContentLink);

    

 Current page could be resolved using some helper methods (assumig that you are inheriting from PageControllerBase - in AlloyTech MVC sample project):

 

protected T GetPage()
{
    var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();
    return pageRouteHelper.Page as T;
}

    

#70751
Edited, Apr 29, 2013 13:42
Vote:
 

Question 1:

You typically make one controller per page type in episerver 7 with mvc.

Consider this:

  • Make a login page type
  • Make a login page type controller
  • Make a view to be displayed via the login page controller's index method
  • The index action can return a view result showing a login form or maybe a redirect result pointing somewhere if the user is authenticated.

Question 2:

I suggest you avoid trying to redirect to controller actions directly if you are not within the same controller. More on that later. If you want to redirect to another controller than the one serving your current page request, my guess is that you actually want to redirect to another page instance in the CMS. If you have a wizard like page with several steps and you would like to post data between steps, that would work just fine:

  • One Index action that serves the standard GET request.
  • A form on the index view might post to the same action or another action on the same page controller.
  • The action method takes in a page data object, and custom view data posted in. The model binder will sort you out most of the time so there should be little need for requesting form and querystring data using http request instances in MVC.


This works fine, and you can get unique URLs for every step. The guys handling your web site tracking will like that.

If you would like to return a RedirectResult pointing to an action, this works just fine if you are within the same controller.

In your login case:

  • Index (GET) showing a form that posts to the same controller action name.
  • Index (POST) doing stuff with the posted data, bound to a nice model
    • Returning redirect to another action on the same controller if all is well.
    • Returning the same index view (with some error messages, perhaps?) if something went wrong.

 

 

#70762
Apr 29, 2013 17:02
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.