I've been looking at Sitecore MVC but I'm stuck at how to handle a case where my page has two controller renderings and each contains a form. I want the individual controllers to handle their HttpPost and return the whole page after post.
I've set up a simple example. Both controllers are similar:
public class ExampleController : Sitecore.Mvc.Controllers.SitecoreController
{
public override ActionResult Index()
{
return View("Index");
}
[HttpPost]
public ActionResult Index(string formPostData)
{
ViewBag.SaveForLater = formPostData;
return Index();
}
}
The views look like this:
@using Sitecore.Mvc
@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{
@Html.AntiForgeryToken()
var term = ViewBag.SaveForLater as string;
if (!string.IsNullOrEmpty(term))
{
<p>Submitted: @term</p>
}
<p>
@Html.Sitecore().FormHandler("Example", "Index")
<input type="text" name="formPostData" placeholder="Enter something" />
<input type="submit" name="submit" value="Search" />
</p>
}
With this setup both forms submit their data but the returned page consists only of the partial view and not the whole page.
If I replace the line @Html.Sitecore().FormHandler("Example", "Index")
with @Html.Sitecore().FormHandler()
then the whole page is returned but the post action for both forms is processed.
Neither scenario is ideal. I must be missing something and would appreciate a pointer.
@Html.Sitecore().FormHandler("Example", "Index")
basically does exactly what you say (i.e. adds the controller and action names as hidden fields) and seems to cause the behaviour you've modeled with an attribute to occur internally. If I include that line only one of the HttpPost actions is processed. But, at the point I return the view from that action (return View(model);
above) I'm losing the rest of the page. It's only the content of that view being rendered in the browser, not the whole page. Any idea why? – Bristling