Search code examples
asp.net-mvcsitecoresitecore-mvcsitecore7.1

Post form returns view not full layout


Running Sitecore 7.1 update 2, I'm creating a login form and here's the setup:

"LoginController" has methods "Index" and "Login" and one view "Index"

A controller rendering is created, the controller field has the "namespace and dll" and the action field has "Index". I'm using MVC Areas, this is why I had to specify the namespace and dll.

A content item has the layout configured with the "Index" controller rendering

The "Index" view has the following code (at first I used the normal MVC begin form but that did not work):

@using (Html.BeginRouteForm(MvcSettings.SitecoreRouteName, FormMethod.Post))
{
    @Html.Sitecore().FormHandler("Login", "Login")
    //form fields...
}

When I submit the form with incorrect credentials I want to show the form (view) again and display the errors, I get the view but stripped out from the layout.

I did not know about the Sitecore FormHandler method until I googled to solve this problem, did not see it the documentation either. Any resources on Sitecore MVC? Content related to Sitecore 7.1 please as most of the examples I found were Sitecore 6 and outdated.

Thanks


Solution

  • With Sitecore you can't use default MVC without any additional logic/code. This is because in ASP.NET MVC you only have one controller/action per request. In Sitecore MVC you can have multiple controllers with multiple actions on one single page request.

    There are several ways of handling form posts in Sitecore MVC. This here a good resource to start. I prefer to have a solution that is pretty much like ASP.NET MVC and I think this is also your idea: With the approach described in my blog you can use normal @Html.BeginForm() and use a custom attribute to specify which controller action should be called. With this you can show error messages if login fails and do whatever you want when login is ok. This also works for multiple forms within one page request.