razor - Is this a bug in WebMatrix PageData? -
i think may have found bug in webmatrix's pagedata, not sure. concerns how pass data partial page calling page.
in webmatrix documentation (tutorials, e.g. "3 - creating consistent look", , example code), pagedata recommended mechanism pass data between pages (e.g. content page layout page, or partial page).
however have found not work other way, pass data partial page calling page. modifying or adding entries in pagedata in partial page, not seem calling page.
cutting right down simplest possible example, in test page may have this:
@{ pagedata["test"] = "initial entry"; } <p>before calling partial page, test value @pagedata["test"]</p> @renderpage("_testpartial.cshtml") <p>after returning calling page, test value @pagedata["test"]</p>
and in _testpartial.cshtml page might have this:
@{ pagedata["test"] = "modified entry"; } <p>in partial page, test value has been modified @pagedata["test"]</p>
the resulting output this:
before calling partial page, test value initial entry
in partial page, test value has been modified modified entry
after returning calling page, test value initial entry
so modification partial page made pagedata lost when return calling page. same occurs if add new entries pagedata in partial page. lost on return calling page.
i don't know if behavior bug, or if intentional, leaves without clean way pass data partial page calling page. there (relatively clean) way that? alternatively, if bug, there work around?
cross-posting response from: http://forums.asp.net/t/1667665.aspx/1?is+this+a+bug+in+webmatrix+pagedata+
this going sound trite, behavior design , not bug. when partial page initalized, copy of pagedata dictionary passed it, why values remain unaffected in original page.
to share values across pages , partials lifecycle of request, use context.items. alternatively, drop in dictionary or expandoobject inside pagedata , use 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"; }
Comments
Post a Comment