How can I display a message in a master page with ViewData?
How can I display a message on the master page. The message is sent by the action.
public A开发者_运维技巧ctionResult DisplayMessage()
{
ViewData["hello"] = "Hello!";
return View();
}
This is actually pretty simple. Just add the following in your controller:
ViewData["PassedToMaster"] = "From content page!";
Then in your MasterPage
you can just add the following code to look for it and if it is there do something with it:
<% if (ViewData["PassedToMaster"] != null)
{ %>
<%= ViewData["PassedToMaster"].ToString() %>
<% } %>
In your view, do the following:
<%= html.encode(ViewData("Hello")) %>
If you want to place this data in another area outside of your view within your master page, you will need to define a new content placeholder.
Master Page:
<div id="somewhereOtherThanYourNormalViewArea">
<asp:ContentPlaceHolder ID="SecondaryContent" runat="server" />
</div>
View:
<asp:Content ID="Content2" ContentPlaceHolderID="SecondaryContent" runat="server">
<%= html.encode(ViewData("Hello")) %>
</asp:Content>
精彩评论