这是 WebMatrix PageData 中的错误吗? [英] Is this a bug in WebMatrix PageData?

查看:48
本文介绍了这是 WebMatrix PageData 中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能在 WebMatrix 的 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.

在 WebMatrix 文档中(教程,例如3 - 创建一致的外观"和示例代码),推荐使用 PageData 作为在页面之间传递数据的机制(例如从内容页面到布局页面,或到部分页面).

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).

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

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>

在 _TestPartial.cshtml 页面中,我们可能有这个:

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>

结果输出是这样的:

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

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

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

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

返回调用页面后,测试值为Initial entry

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

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

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?

推荐答案

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

这听起来很陈词滥调,但这种行为是设计使然,而不是错误.初始化部分页面时,会将 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.

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

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";
}

这篇关于这是 WebMatrix PageData 中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆