这是 WebMatrix PageData 中的错误吗?

发布于 2024-10-19 08:31:17 字数 1236 浏览 6 评论 0原文

我想我可能在 WebMatrix 的 PageData 中发现了一个错误,但我不确定。 它涉及如何将数据从部分页面传递回调用页面。

在 WebMatrix 文档(教程,例如“3 - 创建一致的外观”,以及示例代码),建议使用 PageData 作为在页面之间传递数据的机制(例如,从内容页面到布局页面,或到部分页面)。

然而,我发现这并不总是以另一种方式工作,将数据从部分页面传递回调用页面。在部分页面中修改或添加 PageData 中的条目,似乎无法返回到调用页面。

将其简化为最简单的示例,在测试页面中我们可能有这样的:

@{
    PageData["test"] = "Initial entry";
}

<p>Before calling the partial page, the test value is @PageData["test"]</p>

@RenderPage("_TestPartial.cshtml")

<p>After returning to the calling page, the test value is @PageData["test"]</p>

在 _TestPartial.cshtml 页面中我们可能有这样的:

@{
    PageData["test"] = "Modified entry";
}

<p>In the partial page, the test value has been modified to @PageData["test"]</p>

结果输出是这样的:

调用部分页面之前,测试值为Initial Entry

在部分页面中,测试值已修改为Modified Entry

返回调用页面后,测试值为初始入口

所以部分页面对PageData所做的修改为当您返回调用页面时丢失。如果我们向部分页面中的 PageData 添加新条目,也会发生同样的情况。他们只是在返回调用页面时迷失了方向。

我不知道这种行为是否是一个错误,或者是否是故意的,但它使您没有一种干净的方法将数据从部分页面传递回其调用页面。还有另一种(相对干净的)方法吗?或者,如果这是一个错误,是否有解决方法?

I think I may have found a bug in WebMatrix's PageData, but I am not sure.
It concerns how to pass data from a partial page back to a calling page.

In the WebMatrix documentation (tutorials, e.g. "3 - Creating a Consistent Look", and example code), PageData is recommended as a mechanism to pass data between pages (e.g. from a content page to a layout page, or to a partial page).

However I have found that this does not always work the other way, to pass data from a partial page back to the calling page. Modifying or adding entries in PageData in a partial page, does not seem to get back to the calling page.

Cutting this right down a the simplest possible example, in a test page we may have this:

@{
    PageData["test"] = "Initial entry";
}

<p>Before calling the partial page, the test value is @PageData["test"]</p>

@RenderPage("_TestPartial.cshtml")

<p>After returning to the calling page, the test value is @PageData["test"]</p>

and in the _TestPartial.cshtml page we might have this:

@{
    PageData["test"] = "Modified entry";
}

<p>In the partial page, the test value has been modified to @PageData["test"]</p>

The resulting output is this:

Before calling the partial page, the test value is Initial entry

In the partial page, the test value has been modified to Modified entry

After returning to the calling page, the test value is Initial entry

So the modification that the partial page made to PageData is lost when you return to the calling page. The same occurs if we add new entries to PageData in the partial page. They are just lost on return to the calling page.

I don't know if this behavior a bug, or if is it intentional, but it leaves you without a clean way to pass data from a partial page back to its calling page. Is there another (relatively clean) way to do that? Alternatively, if it is a bug, is there a work around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-10-26 08:31:17

交叉发布我的回复: http://forums.asp.net/t/1667665.aspx/1?Is+this+a+bug+in+WebMatrix+PageData+

这听起来很老套,但这种行为是设计使然,而不是一个错误。当初始化部分页面时,PageData 字典的副本将传递给它,这就是原始页面中的值不受影响的原因。

要在请求的生命周期内跨页面和部分共享值,您可以使用 Context.Items。或者,您可以在 PageData 中放入字典或 ExpandoObject 并使用它来共享值:

@{
    Page["Shared"] = new Dictionary<string, string>();
    Page.Shared["Title"] = "Test";
    @Page["shared"]["Title"]

    @RenderPage("~/Partial.cshtml")

    @Page.Shared["Title"]
}

// Partial.cshtml
@{
    Page.Shared["Title"] = "Updated title";
}

Cross-posting my response from: http://forums.asp.net/t/1667665.aspx/1?Is+this+a+bug+in+WebMatrix+PageData+

This is going to sound trite, but the behavior is by design and not a bug. When a partial page is initalized, a copy of the PageData dictionary is passed to it, which is why the values remain unaffected in the original page.

To share values across pages and partials for the lifecycle of a request, you could use Context.Items. Alternatively, you could drop in a dictionary or an ExpandoObject inside PageData and use that to share values:

@{
    Page["Shared"] = new Dictionary<string, string>();
    Page.Shared["Title"] = "Test";
    @Page["shared"]["Title"]

    @RenderPage("~/Partial.cshtml")

    @Page.Shared["Title"]
}

// Partial.cshtml
@{
    Page.Shared["Title"] = "Updated title";
}
浅笑依然 2024-10-26 08:31:17

您的布局页面正在覆盖从部分页面传递给它的值。要将数据从部分页面传递到布局页面,请使用不同的索引器。

部分页面

@{
  PageData["test"] = "value form partial";
}

布局页面

@{
  PageData["anothertest"] = "value from layout";
}
<p>test value = @PageData["test"]</p>
<p>anothertest value = @PageData["anothertest"]</p>

如果您希望在部分页面未设置该值时显示的布局页面中有一个默认值,那么您可以执行以下

操作 布局页面

@{
  var test = PageData["test"] ?? "Layout default value";
}
<p>test value = @test</p>

部分页面

@{
  PageData["test"] = "partial page value";
}

如果您的部分页面未设置 PageData["test "] 那么将使用“布局默认值”

You layout page is overwriting the value passed to it form the partial page. To pass data from the partial page to the layout page use a different indexer.

Partial page

@{
  PageData["test"] = "value form partial";
}

Layout page

@{
  PageData["anothertest"] = "value from layout";
}
<p>test value = @PageData["test"]</p>
<p>anothertest value = @PageData["anothertest"]</p>

If you want to have a default value in the layout page that is displayed if your partial page doesn't set the value then you can do the following

Layout page

@{
  var test = PageData["test"] ?? "Layout default value";
}
<p>test value = @test</p>

Partial page

@{
  PageData["test"] = "partial page value";
}

If your partial page does not set PageData["test"] then "Layout default value" will be used

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文