如何在ViewData的母版页中显示消息?

时间:2010-08-19 22:05:46

标签: asp.net-mvc master-pages viewdata

如何在母版页上显示消息。该消息由操作发送。

public ActionResult DisplayMessage()
{
    ViewData["hello"] = "Hello!";
    return View();
}

2 个答案:

答案 0 :(得分:2)

这实际上非常简单。只需在控制器中添加以下内容:

ViewData["PassedToMaster"] = "From content page!";

然后在你的MasterPage中,你可以添加以下代码来查找它,如果它在那里做了一些事情:

<% if (ViewData["PassedToMaster"] != null)
   { %>
   <%= ViewData["PassedToMaster"].ToString() %>
<% } %>

答案 1 :(得分:1)

在您看来,请执行以下操作:

<%= 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>
相关问题