我有一個母版頁,其中3個部分視圖呈現,並且它包含一個用於視圖的呈現正文(內容佔位符)。如何在asp net mvc3中將部分視圖的值傳遞給父視圖
我想從我的子視圖傳遞數據(任何字符串)到母版頁上呈現的部分視圖之一。對此,我使用Viewbag
,但在父視圖上無法訪問。
我的代碼如下:
我的母版頁:[Main_Layout.chtml
]
<body>
<div>
@{Html.RenderAction("Header_PartialView", "Home");}
</div>
<div>
<table cellpadding="0" cellspacing="0" width="100%">
<tr valign="top">
<td id="tdLeft" class="lftPanel_Con">
<div>
@{Html.RenderAction("LeftPanel_PartialView", "Home");}
</div>
</td>
<td id="tdRight" width="100%">
<div>
@RenderBody()
</div>
</td>
</tr>
</table>
</div>
<!--start footer-->
<div>
@{Html.RenderAction("Footer_PartialView", "Home");}
</div>
</body>
我的孩子查看:[TestPage1.chtml
]
@{
ViewBag.Title = "TestPage1";
Layout = "~/Views/Shared/Main_LayoutPage.cshtml";
var test1 = ViewBag.testData1;
var test2 = ViewData["testData2"];
}
<h2>TestPage1</h2>
<div>This is only for testing</div>
我的控制器代碼:
public ViewResult TestPage1()
{
ViewBag.testData1 = "My first view bag data";
ViewData["testData2"] = "My second view data";
return View();
}
現在我想訪問我的TestPage的數據Header_PartialView
的意見。
@{
var test = ViewBag.testData1;
var test2 = ViewData["testData2"];
}
'串T傳遞價值itlePrefix =(string)ViewContext.ParentActionViewContext.ViewData [「TitlePrefix」];' – ppumkin