0
votes

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

2

2 Answers

1
votes

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.

2
votes

I think your problem is that you are posting to MVC action directly because of the following line of code:

@Html.Sitecore().FormHandler("Login", "Login")

As sitecore skip the default rendering process, and post directly to the controller Action.

In order to return full layout, Replace That with just :

@Html.Sitecore().FormHandler()

Your "controller rendering" item in sitecore should have "Controller"="Login" and "Action" = "Login", which sitecore uses to decides which controller/Action to post the form to.

Unfortunately, Sitecore does not provide much details yet on how to handle MVC forms, however, there is a MVC version of Sitecore launch, which you can take a look at here , there are some good MVC examples there, it might help.