Sitecore MVC Controller and Forms - ActionResult n

2019-09-09 20:55发布

问题:

Sitecore 7.1v1, most recent Glass Mapper, MVC4. When we submit the form POST, we get no layout with the return View. I'd prefer not to have to redirect to another page since this is supposed to be a wizard-like experience. This is also lightweight enough not to require Ajax, although we could use it as a last resort. I can't find who to make sure that while returning the View that we get the layout as well. I'm new to Sitecore MVC and pretty new at MVC in general. The PageBase that's referenced is a Custom Model using Glass.

We have the following Controller Rendering:

public class RegistrationController : Controller
{
    [HttpGet]
    public ActionResult VerifyAccount()
    {

        return View("~/Views/Public/Wizards/Registration/VerifyAccount.cshtml",
            new SitecoreContext().GetCurrentItem<PageBase>());
    }
    [HttpPost]
    public ActionResult CreateProfile()
    {
        ViewBag.Name = Request["VerificationType"];
        ViewBag.Step = 2;
        return View("~/Views/Public/Wizards/Registration/CreateProfile.cshtml",
            new SitecoreContext().GetCurrentItem<PageBase>());
    }
}

The default action for this is VerifyAccount(). This renders as expected. The initial view is as follows:

 @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Public.Model.GlassModel.Primary.PageBase>

<div>
<h3>@Editable(a => a.Title)</h3>
<p>
    @Editable(a => a.Description)
</p>
<hr />
@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{
    @Html.Sitecore().FormHandler("Registration", "CreateProfile")
    @Html.ValidationSummary(true, "Verification Not Selected.")
    <fieldset>
        @Sitecore.Globalization.Translate.Text("$Registration.VerificationTitle")
        @{ var validations = new SitecoreContext().GetItem<GlassFrameBase>(Guid.Parse("{3694FC43-3DB7-470A-A1E9-2649856AAF10}"));}
        <select id="VerType" name="VerificationType">
            @foreach (var validation in validations.GetChildren<VerificationMethod>())
            {
                <option value="@validation.MethodValue">@validation.MethodName</option>
            }
        </select>
        <input type="submit" value="Next" />
    </fieldset>
}

This posts back to the CreateProfile() Method. This part works great. The only issue is that when it returns the view this time, it returns just the view without the layout.

The final view is as follows:

@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{
@Html.Sitecore().FormHandler()
<p>
    <b>Verification Type Was: </b>@ViewBag.Name
</p>
<p>@ViewBag.Step</p>
<input type="hidden" value="ThisIsATest" name="TestHidden" id="TestHidden"/>
<input type="submit" name="back" value="Back" /><br />
<input type="submit" name="next" value="Next" />
}

Everything else is working exactly as expected but I'm missing something important that loads the Layout on the return trip.

回答1:

I have noticed this before as well and I think it relates to this line:

@Html.Sitecore().FormHandler("Registration", "CreateProfile")

It seems to bypass the standard rendering pipeline and just call the target action. I have written a blog post on how you can control calls to different action on multiple controllers. this might help:

http://www.experimentsincode.com/?p=425



回答2:

Try changing the return type of CreateProfile from ActionResult to PartialViewResult, and then return View("... to return PartialView("...

Also, here's a post about what you can return for Sitecore Controller Renderings. http://mhwelander.net/2014/04/09/sitecore-controller-rendering-action-results-what-can-i-return/

I haven't looked deeply into form posting with Controller Renderings, but if the above suggestion doesn't work then maybe consider the execution lifestyle used in Sitecore MVC (mentioned in the post).