【问题标题】:EPiServer MVC currentPage issuesEPiServer MVC currentPage 问题
【发布时间】:2014-11-19 22:29:56
【问题描述】:

我目前在使用以下 coden-ps 时遇到了一些问题,这些问题看起来与我几乎相同,但行为不同。

这些 sn-ps 来自我一直从事的两个不同项目,它们的构建方式相同,但只有一个可以正常工作。

这些是我输入控制器的表单:

表格 1,在 _Layout 文件中的 twitter 引导下拉菜单中:

    @using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post))
    {
    <li>
        <button type="submit" class="dropdownButton">Redigera Profil</button>
    </li>
    }

Form 2,尝试了不同的位置,但现在它位于 Block 视图中的表格中:

    <td>
        @using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post))
        {
            <button type="submit">Redigera profil</button>
        }
    </td>

两者看起来非常相似,对吧?

现在是控制器

控制器 1:

    public ActionResult EditProfile(ProfilePage currentPage)
    {
        var model = new ProfilePageViewModel(currentPage);
        model.CurrentUser = ConnectionHelper.GetUserInformationByEmail(User.Identity.Name);
        return View("EditProfile", model);
    }

控制器 2:

    public ActionResult EditProfile(ProfilePage currentPage)
    {
        ProfilePageViewModel model = new ProfilePageViewModel(currentPage);
        model.currentUser = ConnectionHelper.GetCurrentUserByEmail(User.Identity.Name);
        return View("EditProfile", model);
    }

也几乎相同。

我在两个项目中添加了相同的路由:

    protected override void RegisterRoutes(RouteCollection routes)
    {
        base.RegisterRoutes(routes);
        routes.MapRoute(
          "Default",                                             
          "{controller}/{action}/{id}",                           
          new { controller = "Home", action = "Index", id = "" });
    }

现在问题来了:

表格 1 和控制器 1 工作正常,并且接收 ProfilePage currentPage 没有任何问题,但表格 2 和控制器 2 不起作用并获得空值。 正如我之前所说的,表格 1 发布在 _Layout 页面上,表格 2 发布在一个块中,该块在 mvc @section 中呈现。我不认为这是问题所在,因为我试图从页面的不同部分访问控制器,但它在任何地方都不起作用 - 但在另一个项目中,它无处不在,这让我发疯了。

有人知道为什么会这样吗?我在调试时已经对它们进行了调试,但唯一的区别是一个有效,另一个无效。

提前致谢

去性

编辑:

这里我渲染了一个叫做“内容”的部分,几乎所有的东西都会被渲染。

    <div id="content">
     @RenderBody()
     @RenderSection("content", false)
    </div>

我的起始页有一个用于块的 ContentArea,在此部分中呈现:

@model Intranet.Models.ViewModels.StartPageViewModel
@section content{
   @if (User.Identity.IsAuthenticated)
   {
       <div class="contentArea">
           @Html.PropertyFor(x => x.MainContentArea)
       </div>
   }
}

这里是继承自 BlockController 的控制器:

public class ProfileBlockController : BlockController<ProfileBlock>
{
    public override ActionResult Index(ProfileBlock currentBlock)
    {
        ProfileBlockViewModel model;
        if (currentBlock != null)
        {
            model = new ProfileBlockViewModel(currentBlock);
        }
        else
        {
            model = (ProfileBlockViewModel)Session["model"];
        }
        model.CurrentUser = ConnectionHelper.GetCurrentUserByEmail(User.Identity.Name);
        var availableStatuses = ConnectionHelper.GetAllOfficeStatuses();
        availableStatuses.Remove(model.CurrentUser.OfficeStatus);
        model.AvailableStatusChanges = availableStatuses;
        Session["model"] = model;

        return PartialView(model);
    }

}

【问题讨论】:

  • Controller 2 ineherit from 是什么
  • 两者都继承 ProfilePageController : PageController
  • 该块是如何渲染的?通常一个块会由继承 BlockController 的控制器渲染?
  • 是的,请检查我的编辑。

标签: c# asp.net-mvc episerver


【解决方案1】:

currentPage”路由值(即参数)只会由EPiServer的页面路由设置。它在块控制器中始终为空。

但是,您可以通过以下方式在块控制器中获取当前请求的页面:

PageRouteHelper.Page

如果块是作为个人资料页面请求的一部分呈现的,您将能够通过 PageRouteHelper 获取该个人资料页面。

【讨论】:

  • 我认为您误解了:我从块提交表单,但我想登陆 ProfilePage,其中 currentPage 在 ProfilePageController 中为空。如果我使用 PageRouteHelper.Page 我会得到 StartPage,这是在块所在的 ContentArea 内发生帖子的位置。
  • 块控制器中的“currentPage”操作方法参数将始终为空,除非您实际上手动包含“currentPage”路由值?当您从起始页发布时,我不确定您期望块控制器中的“currentPage”是什么?
  • 我想知道为什么相同的操作结果/控制器不起作用。您介意详细说明我如何使用 PageRouteHelper 吗?因为我只得到我来自的页面,而不是使用该代码的请求页面。 BlockController 有一个 currentBlock 参数,ProfilePage 的 PageController 有 currentPage 参数,当从 Block 视图中在 PageController 中收到帖子时,该参数为 null。
  • 当块控制器的 action 方法处理帖子时,此时的“当前页面”是发布帖子的页面(可通过 PageRouteHelper 获得)。只有在请求 page 时,才会为控制器设置“currentPage”参数,当块控制器处理来自表单的帖子时,情况并非如此。我认为你需要修改你的逻辑,似乎有一些 MVC 混淆。 :)
  • 关于我可以做些什么来解决这个问题的任何提示?我的意思是我不能发布到 ProfileBlockController 并尝试接收 ProfilePage 然后将该页面重定向到 ProfilePageController,因为它会变成和我现在一样的方式,对吗?发布到 ProfilePageController 也没有收到页面,我可以在表单中添加指定类型的 Content/PageReference 节点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2011-09-05
  • 2010-11-14
相关资源
最近更新 更多