0
如何在母版頁上顯示消息。該消息由動作發送。如何在ViewData的母版頁中顯示消息?
public ActionResult DisplayMessage()
{
ViewData["hello"] = "Hello!";
return View();
}
如何在母版頁上顯示消息。該消息由動作發送。如何在ViewData的母版頁中顯示消息?
public ActionResult DisplayMessage()
{
ViewData["hello"] = "Hello!";
return View();
}
在你看來,做到以下幾點:
<%= html.encode(ViewData("Hello")) %>
如果您希望將這些數據在您的母版頁中的視圖之外的另一個領域,你需要定義一個新的內容佔位符。
母版頁:
<div id="somewhereOtherThanYourNormalViewArea">
<asp:ContentPlaceHolder ID="SecondaryContent" runat="server" />
</div>
查看:
<asp:Content ID="Content2" ContentPlaceHolderID="SecondaryContent" runat="server">
<%= html.encode(ViewData("Hello")) %>
</asp:Content>
這其實是非常簡單的。只需添加下面的控制器:
ViewData["PassedToMaster"] = "From content page!";
然後在你的MasterPage
你可以添加以下代碼來尋找它,如果它的存在,用它做什麼:
<% if (ViewData["PassedToMaster"] != null)
{ %>
<%= ViewData["PassedToMaster"].ToString() %>
<% } %>