【问题标题】:Sitecore MVC Controller and Forms - ActionResult not rendering Layout on postbackSitecore MVC 控制器和表单 - ActionResult 未在回发时呈现布局
【发布时间】:2014-04-22 15:43:30
【问题描述】:

Sitecore 7.1v1,最新的 Glass Mapper,MVC4。当我们提交表单 POST 时,我们没有得到带有返回视图的布局。我宁愿不必重定向到另一个页面,因为这应该是一种类似向导的体验。这也是轻量级的,不需要 Ajax,尽管我们可以将它用作最后的手段。我找不到谁来确保在返回 View 时我们也获得了布局。我是 Sitecore MVC 的新手,一般来说在 MVC 上也很新。引用的 PageBase 是使用 Glass 的自定义模型。

我们有以下控制器渲染:

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>());
    }
}

此操作的默认操作是 VerifyAccount()。这按预期呈现。初步看法如下:

 @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>
}

这回发到 CreateProfile() 方法。这部分效果很好。唯一的问题是,当它返回视图时,它只返回没有布局的视图。

最终视图如下:

@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" />
}

其他一切都按预期工作,但我错过了在回程中加载布局的重要内容。

【问题讨论】:

    标签: asp.net-mvc-4 sitecore glass-mapper


    【解决方案1】:

    我之前也注意到了这一点,我认为它与这条线有关:

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

    它似乎绕过了标准的渲染管道,只是调用了目标动作。我写了一篇关于如何控制对多个控制器上不同操作的调用的博客文章。这可能会有所帮助:

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

    【讨论】:

    • 这是你的答案。您需要 POST 到 Sitecore 项目,而不是 MVC 操作。我会摆脱 BeginRouteForm 和 FormHandler 的东西,只是放一个
      指向定义布局的 Sitecore 项目。然后使用 Michael 的解决方案将多个操作应用于一项。效果很好。
    • 完美,这正是我所需要的。谢谢!
    【解决方案2】:

    尝试将 CreateProfile 的返回类型从ActionResult 更改为PartialViewResult,然后将return View("... 更改为return PartialView("...

    此外,还有一篇关于您可以为 Sitecore 控制器渲染返回什么的帖子。 http://mhwelander.net/2014/04/09/sitecore-controller-rendering-action-results-what-can-i-return/

    我还没有深入研究过使用控制器渲染的表单发布,但如果上述建议不起作用,那么可以考虑 Sitecore MVC 中使用的执行生活方式(在帖子中提到)。

    【讨论】:

    • 谢谢,我也会看看这个。我已将 Mike 的回复标记为答案,但也为此 +1 了,因为它是很好的信息。
    猜你喜欢
    • 2017-01-28
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多