Is this a bug in WebMatrix PageData?

前端 未结 2 1824
难免孤独
难免孤独 2021-01-14 09:17

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

2条回答
  •  终归单人心
    2021-01-14 09:43

    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();
        Page.Shared["Title"] = "Test";
        @Page["shared"]["Title"]
    
        @RenderPage("~/Partial.cshtml")
    
        @Page.Shared["Title"]
    }
    
    // Partial.cshtml
    @{
        Page.Shared["Title"] = "Updated title";
    }
    

提交回复
热议问题