简体   繁体   English

如何在MVC2中将数据从子视图传递到父视图?

[英]How do I pass data from child view to parent view in MVC2?

Using .Net MVC2, I would like to pass information from a child view to its parent view. 使用.Net MVC2,我希望将信息从子视图传递到其父视图。 Initially, I thought I could use the ViewData collection, but this seems to only work in one direction: from the parent to the child. 最初,我以为我可以使用ViewData集合,但这似乎只在一个方向上起作用:从父级到子级。

Below is a simplified version of what I am attempting in an effort to pass data from the child view back to the parent view. 下面是我试图将数据从子视图传递回父视图的简化版本。 When running the Visual Studio debugger, I see the ViewData dictionary while stepping through both the parent and child views. 运行Visual Studio调试器时,在逐步浏览父视图和子视图时,都会看到ViewData字典。 But the statement which should add an item to the dictionary in the child view does not seem to work. 但是应该在子视图中向字典添加项的语句似乎不起作用。 After returning to the parent view, the ViewData dictionary appears the same as before calling the child. 返回到父视图后,ViewData词典的显示与调用子视图之前的视图相同。

In the parent view: 在父视图中:

<div>
    <%
        List<People> ManagerList = ViewData["ManagerList"];
        ViewData["Section"] = "Managers";
        Html.RenderPartial("OrgTable", ManagerList);
        bool didTableRenderAnyRecords = ViewData["TableResult"];
    %>
</div>
...
<div>
    <%
        if (!didTableRenderAnyRecords) {
    %>
    <p>Sorry, no records found</p>
    <%
        }
    %>
</div>

In the child view 在子视图中

<table>
    <thead> ... </thead>
    <tbody>
    <%
        bool validRecordDisplayed = false;
        foreach(var manager in Model) {
            if (manager != null) {
               validRecordDisplayed = true;
    %>
    <tr> ... </tr>
    <%
            }
        }
        ViewData["TableResult"] = validRecordDisplayed;
    %>
    </tbody>
</table>

I think you can use ViewBag.SomeProperty here. 我认为您可以在此处使用ViewBag.SomeProperty。 You can also try and use MVC sections (although I am not sure if those are available in MVC2). 您也可以尝试使用MVC部分(尽管我不确定MVC2中是否可用这些部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM