Is this a bug in WebMatrix PageData?

纵然是瞬间 提交于 2019-12-01 06:56:19

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/5127284/is-this-a-bug-in-webmatrix-pagedata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!