C# EPiServer MVC当前页面问题
我目前在以下代码片段中遇到了一些问题,这些代码片段看起来与我几乎相同,但行为不同 这些代码片段来自我一直在从事的两个不同的项目,它们以相同的方式构建,但其中只有一个工作正常 以下是我输入控制器的表格: 表单1,在twitter引导下拉菜单中,位于_布局文件中:C# EPiServer MVC当前页面问题,c#,asp.net-mvc,episerver,C#,Asp.net Mvc,Episerver,我目前在以下代码片段中遇到了一些问题,这些代码片段看起来与我几乎相同,但行为不同 这些代码片段来自我一直在从事的两个不同的项目,它们以相同的方式构建,但其中只有一个工作正常 以下是我输入控制器的表格: 表单1,在twitter引导下拉菜单中,位于_布局文件中: @using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post)) { <li> <button type
@using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post))
{
<li>
<button type="submit" class="dropdownButton">Redigera Profil</button>
</li>
}
控制器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发布在_布局页面上,表格2发布在mvc@节中呈现的块中。我不认为这是个问题,因为我试图从页面的不同部分访问控制器,但它在任何地方都不起作用——但在另一个项目中,它在任何地方都起作用,这让我发疯
有人知道为什么会这样吗?在调试过程中,我对这两种方法都进行了介绍,但唯一的区别是一种方法有效,而另一种方法无效
提前谢谢
苔丝
编辑:
在这里,我渲染一个名为“内容”的部分,其中几乎所有内容都将被渲染
<div id="content">
@RenderBody()
@RenderSection("content", false)
</div>
@RenderBody()
@RenderSection(“内容”,false)
我的startpage有一个块的ContentArea,在本节中呈现:
@model Intranet.Models.ViewModels.StartPageViewModel
@section content{
@if (User.Identity.IsAuthenticated)
{
<div class="contentArea">
@Html.PropertyFor(x => x.MainContentArea)
</div>
}
}
@model Intranet.Models.ViewModels.StartPageViewModel
@章节内容{
@if(User.Identity.IsAuthenticated)
{
@Html.PropertyFor(x=>x.MainContentArea)
}
}
以下是从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);
}
}
公共类ProfileBlockController:BlockController
{
公共覆盖操作结果索引(ProfileBlock currentBlock)
{
profileblockview模型;
如果(currentBlock!=null)
{
模型=新的ProfileBlockViewModel(currentBlock);
}
其他的
{
模型=(ProfileBlockViewModel)会话[“模型”];
}
model.CurrentUser=ConnectionHelper.GetCurrentUserByEmail(User.Identity.Name);
var availableStatuses=ConnectionHelper.GetAllOfficeStatuses();
AvailableStatus.Remove(model.CurrentUser.OfficeStatus);
model.AvailableStatusChanges=availableStatuses;
会话[“模型”]=模型;
返回局部视图(模型);
}
}
当前页面的路由值(即参数)将仅由EPiServer的页面路由设置。在块控制器中,它将始终为空
但是,您可以通过以下方式在块控制器中获取当前请求的页面:
PageRouteHelper.Page
如果块是作为配置文件页面请求的一部分呈现的,则您可以通过PageRouteHelper获取该配置文件页面
Controller 2
在另一个页面中做了什么Controller 2继承了ProfilePageController:PageController该块是如何呈现的?通常情况下,块将由继承BlockController的控制器呈现?是的,请检查我的编辑。我想你误解了:我正在从块提交表单,但我想在ProfilePage上着陆,其中当前页面在ProfilePage控制器中为空。如果我使用PageRouteHelper.Page,我会得到StartPage,这是块所在的ContentArea中post发生的位置。块控制器中的“currentPage”操作方法参数将始终为空,除非您实际上手动包含“currentPage”路由值?我不确定当您从起始页发布时,您希望块控制器中的“currentPage”是什么?我想知道的是为什么相同的actionresults/控制器不起作用。你介意告诉我如何使用PageRouteHelper吗?因为我只获得我来自的页面,而不是使用该代码获得请求的页面。BlockController有一个currentBlock参数,ProfilePage的PageController有一个currentPage参数,当在PageController中从块视图接收到post时,该参数将变为null。当Block controller的操作方法处理post时,此时的“当前页面”就是进行post的页面(可通过PageRouteHelper获得)“currentPage”参数将仅在请求页面时为控制器设置,而当块控制器处理表单中的帖子时则不是这样。我认为您需要修改逻辑,似乎存在一些MVC混淆。:)关于如何解决此问题,有什么提示吗?我的意思是,我不能发布到ProfileBlockController并尝试接收ProfilePage,然后将该页重定向到ProfilePageController,因为结果将与我现在的结果相同,对吗?发布到ProfilePageController也不会收到页面,我可以在表单中添加指定类型的内容/页面引用节点吗?
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);
}
}
PageRouteHelper.Page